homepage Welcome to WebmasterWorld Guest from 54.226.213.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Timer not accurate
Timer not accurate, off my minutes not seconds
fredfletcher




msg:4425241
 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()">


 

Fotiman




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

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).

fredfletcher




msg:4425251
 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.

Fotiman




msg:4425257
 9:43 pm on Mar 5, 2012 (gmt 0)

Here's an example that synchronizes every 10 seconds:
[jsfiddle.net...]

Fotiman




msg:4425258
 9:45 pm on Mar 5, 2012 (gmt 0)

Oh, that's weird.

fredfletcher




msg:4425261
 9:56 pm on Mar 5, 2012 (gmt 0)

Thank you, I will give that a try. Very much appreciated

fredfletcher




msg:4425277
 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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved