Welcome to WebmasterWorld Guest from 54.159.250.110

Forum Moderators: open

Message Too Old, No Replies

Timer not accurate

Timer not accurate, off my minutes not seconds

   
8:59 pm on Mar 5, 2012 (gmt 0)



Hi, I have a question about an automatic redirection timer. Is there a script that is more accurate than the one shown below?

I ask this, because the one I am presently using is not accurate at all. I can live with a 40-60 second difference, but in using FF 8.0.1, it won't kick in till about 2-5 minutes later.

I did search in the forums for quite a while, but did not find anything.

Here is my code:

[JS]

var limit="60:00"
if (document.images){
var parselimit=limit.split(":")
parselimit=parselimit[0]*60+parselimit[1]*1
}
function begintimer(){
if (!document.images)
return
if (parselimit==1)
window.top.location="redirection_page.htm"
else{
parselimit-=1
curmin=Math.floor(parselimit/60)
cursec=parselimit%60
if (curmin!=0)
curtime=curmin+" minutes and "+cursec+" seconds left"
else
curtime=cursec+" seconds left"
window.status=curtime
setTimeout("begintimer()",1000)
}

---

[HTML]

<body onLoad="begintimer()">

9:32 pm on Mar 5, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Not sure what you mean by "it won't kick in till about 2-5 minutes later". Later than what? And what won't kick in? You're starting this onload, which means it won't start until all DOM elements have loaded, including all images, scripts, etc. So if you have advertising code that is taking a long time to load, that could affect this script.

Another approach might be to create a Date object to act as a timestamp, and then periodically sync with that. For example, over time your script will probably take longer than 60 minutes, simply because of the overhead with the script. However, you could keep some separate counter that on every 60th iteration re-synchronizes with the timestamp (creates a new Date object, calculates the difference from your original timestamp, and adjusts the numbers on your countdown appropriately).
9:37 pm on Mar 5, 2012 (gmt 0)



When the 60 minutes are up (I kept tabs on the time), the script does not redirect the page till a few minutes later. I do not have any images or advertising code at all, just text.
9:43 pm on Mar 5, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Here's an example that synchronizes every 10 seconds:
[jsfiddle.net...]
9:45 pm on Mar 5, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Oh, that's weird.
9:56 pm on Mar 5, 2012 (gmt 0)



Thank you, I will give that a try. Very much appreciated
10:56 pm on Mar 5, 2012 (gmt 0)



One hour has passed and I have some very good news, it worked beautifully! Right on the button :) Thanks again, I appreciate it very much!