Firefox runs setTimeout function too fast (or is Date.getTime () turned off?)

I am having a strange problem in Firefox 12. setTimeout () does not seem to always wait for the appropriate length. Or maybe it's a millisecond date that is not jive?

Check out this script. Essentially, a 100 ms setTimeout seems to work anywhere between 80 ms and 110 ms. I can understand more based on the explanation of John Resig timers . But less?

You may need to update it once or twice to see the problem, as it sometimes works correctly the first time it starts. It seems to work in IE and Chrome.

Here is the code I use in my fiddle:

var txt = '', TIMEOUT_LENGTH = 100, _now; now = Date.now || function() { return new Date().getTime() }; function log(time) { c = time < 100? 'class="error"' : ''; $('#log').append('<p '+c+'>waited ' + time + '</p>'); } function defer() { var d = $.Deferred(), start = now(); setTimeout(function() { d.resolve(now() - start); }, TIMEOUT_LENGTH); return d.promise(); } for (var i = 0; i < 20; i++) { defer().then(log); } 

Here's a sample of a dodgy conclusion:

enter image description here

Information about my browser:

enter image description here

And thanks for reading my question! I hope someone can shed some light on this.

ADDITIONAL INFORMATION

I worked on the problem using setInterval () and checking each step to see if the required time had passed. Check out this script .

However, I am still very interested to know if anyone can shed light on the source of the problem.

+6
source share
3 answers

Yes. setTimeout accuracy is based on many factors and is not guaranteed to be always executed at the time you specify.

I can’t say this with any authority, but I’ll be afraid that Firefox, trying to seem faster, will temporarily speed up the JS engine so that everything is in motion (which is interesting because, in my experience, timer-based functions actually running slower first in my version of firefox).

Neither setTimeout nor setInterval promise that they will be executed at the exact time, as indicated in the link you provided. However, with setInterval you get the advantage of a timer loop doing everything possible to “fix itself”, catching up if it lags too far, so whatever you try to do, it might be more appropriate.

Anyway, here are my results on my Macbook 10.6.8:

Firefox 5.0.1:

 waited 92 waited 92 waited 93 waited 93 waited 93 waited 93 waited 93 waited 94 waited 93 waited 93 waited 93 waited 93 waited 94 waited 94 waited 94 waited 94 waited 94 waited 95 waited 96 waited 96 

Safari 5.1.5:

 waited 100 waited 104 waited 104 waited 103 waited 104 waited 104 waited 104 waited 104 waited 104 waited 104 waited 104 waited 104 waited 104 waited 104 waited 104 waited 104 waited 104 waited 104 waited 104 waited 104 

Chrome 19.0.1084.52:

 waited 101 waited 103 waited 103 waited 104 waited 104 waited 103 waited 103 waited 103 waited 103 waited 103 waited 103 waited 103 waited 103 waited 103 waited 104 waited 104 waited 104 waited 104 waited 104 waited 104 
+3
source

JavaScript is synchronous. The browser will add your setTimeout to the queue and execute them after

  • The countdown is over
  • He completed any other tasks that are in the queue at the end of the countdown.
+1
source

I wrote the following function to make sure my code executes after at least a given number of milliseconds. I use it instead of setTimeout

 mySetTimeout=function(f,t){ var endTime=new Date().valueOf()+t; setTimeout(function(){ var now=new Date().valueOf(); if(now<endTime){ mySetTimeout(f,endTime-now); }else{ f(); } },t); }; 
+1
source

Source: https://habr.com/ru/post/917332/


All Articles