Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write a fast timer in Javascript?
    text
    copied!<p>I need a very fast timer in Javascript. The timer resolution is considered to be 10 milliseconds. I already implemented a working timer, but the problem is, that the time is always depending on how many other ressources are running in the Browser. And when an animated GIF is running, my timer won't work at all. This is the code (working!):</p> <pre><code>var timer = new Timer(); function Timer(){ var time = 0; //Every 10ms we are incrementing the timer: setInterval(function(){ time=time+1; },10); this.getTime=function(){return time;} } function testTimer(){ var counter = 0; var resultsum = 0; var everytensum = 0; setInterval(function(){ counter = counter + 1; window.timeoutTesterRunning = true; var timeoutTester = new TimeoutTester(); setTimeout(function(){ console.log(counter+": "+timeoutTester.getResult()); resultsum = resultsum+timeoutTester.getResult(); console.log(resultsum); if(counter%10==0){ console.log("Counting last 10 seconds: "+(resultsum-everytensum)); console.log("Counting last "+counter+" seconds: "+resultsum); everytensum = resultsum; } },1200) }, 3000); } function TimeoutTester(){ var result; var snap_time1 = timer.getTime(); setTimeout(function(){ var snap_time2 = timer.getTime(); result = snap_time2-snap_time1; },1000); this.getResult=function(){return result;} } </code></pre> <p>Now the results from the console.log (the sums are in brackets):</p> <pre><code>[10:10:54.466] 1: 100 (100) [10:10:57.466] 2: 100 (200) [10:11:00.466] 3: 100 (300) [10:11:03.466] 4: 100 (400) [10:11:06.466] 5: 100 (500) [10:11:09.465] 6: 100 (600) [10:11:12.466] 7: 101 (701) [10:11:15.467] 8: 100 (801) [10:11:18.467] 9: 100 (901) [10:11:21.467] 10: 100 (1001) [10:11:21.476] Counting last 10 seconds: 1001 [10:11:24.467] 11: 100 (1101) [10:11:27.468] 12: 100 (1201) [10:11:30.467] 13: 100 (1301) [10:11:33.478] 14: 60 (1361) //Animated Gif started: Less countings! ("Time runs longer") [10:11:36.476] 15: 57 (1418) [10:11:39.482] 16: 58 (1476) [10:11:42.472] 17: 61 (1537) [10:11:45.474] 18: 56 (1593) [10:11:48.484] 19: 48 (1641) [10:11:51.470] 20: 55 (1696) [10:11:51.476] Counting last 10 seconds: 695 [10:11:51.482] Counting last 20 seconds: 1696 </code></pre> <p>The counter runs quite regular. But when the Browser is busy (by a animated GIF), Javascript has no time to set the intervals correctly. The function <code>Timer()</code> just doesn't receive the time it needs to increment <code>var time</code>, so the values are far to low (when I first started testing, the values were even lower at ~40, but I was able to rise them to ~55 by using functional implementation).</p> <p>Anybody knows how we could get that Javascript timer working? Or is it just impossible to prevent the browser killing the timer?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload