RockinPurdy Posted January 26, 2008 Share Posted January 26, 2008 Hey there, I have a script that I've made to countdown to the next 10 minute interval as shown below: function TimeLeft() { var Time = new Date(); var Minutes = Time.getMinutes(); var Seconds = Time.getSeconds(); var MinutesLeft = 0; var SecondsLeft = 0; if (Minutes < 10) { MinutesLeft = 10 - Minutes; } else if (Minutes < 20) { MinutesLeft = 20 - Minutes; } else if (Minutes < 30) { MinutesLeft = 30 - Minutes; } else if (Minutes < 40) { MinutesLeft = 40 - Minutes; } else if (Minutes < 50) { MinutesLeft = 50 - Minutes; } else if (Minutes < 60) { MinutesLeft = 60 - Minutes; } SecondsLeft = 60 - Seconds; if (SecondsLeft < 26) { MinutesLeft = MinutesLeft - 1; SecondsLeft = 60 - (26-SecondsLeft); } else { SecondsLeft = SecondsLeft - 26; } if (SecondsLeft < 10) { SecondsLeft = "0" + SecondsLeft; } document.getElementById('time').innerHTML="Share Value Updates in "+MinutesLeft+":"+SecondsLeft; Timeout = setTimeout('TimeLeft()',500); } The problem is, it freezes when it gets to 1:00... and then when I refresh nothing comes up.. but then when I refresh some more times it will show 10:26 which it shouldn't. Btw, the 26 is an offset.. ie. the countdown was 26 seconds off than what it should have been. Quote Link to comment Share on other sites More sharing options...
taith Posted January 26, 2008 Share Posted January 26, 2008 as an fyi:i'm assuming that your getting your starting time from a server language(php?)... and its taking time before it hits the browser before it can start...(thats where the 26~ seconds comes from... also... since it takes js some time to run through lines of code "Timeout = setTimeout('TimeLeft()',500);" will be HIGHLY inaccurate... if you were to use "setInterval()" it'd be FAR more accurate :-) Quote Link to comment Share on other sites More sharing options...
RockinPurdy Posted January 27, 2008 Author Share Posted January 27, 2008 Sorry Im kinda new with Javascript. Could you explain how I would use that? Quote Link to comment Share on other sites More sharing options...
taith Posted January 27, 2008 Share Posted January 27, 2008 function TimeLeft() { var Time = new Date(); var Minutes = Time.getMinutes(); var Seconds = Time.getSeconds(); var MinutesLeft = 0; var SecondsLeft = 0; if (Minutes < 10) MinutesLeft = 10 - Minutes; else if (Minutes < 20) MinutesLeft = 20 - Minutes; else if (Minutes < 30) MinutesLeft = 30 - Minutes; else if (Minutes < 40) MinutesLeft = 40 - Minutes; else if (Minutes < 50) MinutesLeft = 50 - Minutes; else if (Minutes < 60) MinutesLeft = 60 - Minutes; SecondsLeft = 60 - Seconds; if (SecondsLeft < 26) { MinutesLeft = MinutesLeft - 1; SecondsLeft = 60 - (26-SecondsLeft); } else SecondsLeft = SecondsLeft - 26; if (SecondsLeft < 10) SecondsLeft = "0" + SecondsLeft; document.getElementById('time').innerHTML="Share Value Updates in "+MinutesLeft+":"+SecondsLeft; } Timeout = setInterval('TimeLeft()',1000); that way the function will loop every 1 second regardless of how long it takes to process the function... still... javascript is based off of the computer's time... it'll still be a little bit wrong(depending on the computer it'll run a little faster/slower then intended)... it shouldnt be a dependant on the rest of your site... just an addon Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.