Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here's a strange one:

I was working on synchronizing a JavaScript timer with a remote server and noticed my timer (based on the Date() object) was gaining about 12ms relative to my remote server on each page refresh. I narrowed it down to the JavaScript alert() function.

I created the following test and verified with 2 synchronized clocks that my system time will advance 1 second every 83-84 page refreshes. Has anybody seen this before?

<html>
<head>
</head>
<body>
<script type="text/javascript">
  alert('hello');
</script>
</body>
</html>

I'm using IE7 on XP SP3. Please tell me I'm not just going crazy!


EDIT:

I'm really not concerned about the accuracy of javascript timers. The real issue is that the JavaScript alert() function call is advancing my actual system time by 12ms.

share|improve this question
3  
Try refreshing it 88 times during a lighting storm. –  MooGoo Aug 5 '10 at 14:28
4  
Just tried this (IE7, XP SP3) and to my surprise, get the same results - system time is advanced with each alert. Not sure if it's by the same amount as the OP as I didn't count the refreshes, but it definitely advances. –  Iridium Aug 5 '10 at 14:32
7  
How do you know it's the system clock advancing and not the rest of the universe losing time? –  Jason Orendorff Aug 5 '10 at 14:39
2  
@Jason: Maybe the javascript alert causes my computer to approach the speed of light? –  Derek Aug 5 '10 at 14:41
3  
Did you try synchronizing the clocks, and then, not really do anything, or at least browse it like you would normally do, and then testing how far they're out of sync? (You know, as a control.) –  JayC Apr 30 '12 at 13:15
show 9 more comments

2 Answers

Well,

most likely you are crazy, since you are counting on JavaScript timers to be precise.

This explains a lot of that: http://ejohn.org/blog/accuracy-of-javascript-time/

share|improve this answer
11  
But the OP claims that his system time gets altered. This is something else, isn't it? –  Pekka 웃 Aug 5 '10 at 14:14
1  
What I'm talking about isn't a delay in my javascript timer -- My actual system time is being modified by the execution of the alert() function. I've refreshed this page a couple hundred times and now when I pull up my system clock it is 5 seconds ahead of my watch that I had just synchronized with my system. –  Derek Aug 5 '10 at 14:16
2  
@Derek: I always knew that JavaScript is evil ;) (sorry for not providing a more useful comment). –  Felix Kling Aug 5 '10 at 14:25
add comment

Are you sure alert() is causing your systems time to change and not ntp?

Network Time Protocol

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.