Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple timer, and setinterval

What's the best way to create a timer in JS?

I've been using this so far:

var sec = 0;
setInterval(function (){sec +=1}, 1000);

I've noticed that, when I need miliseconds, it slows down by a lot. On browser tab changes, it completely stops.

var milisec = 0;
setInterval(function (){milisec +=1}, 1);

I'm looking for a better way to handle this, which will also continue to work when the browser window is changed.

like image 938
Mia Avatar asked Aug 23 '26 04:08

Mia


1 Answers

With milliseconds, the resolution of the timer isn't large enough. In most cases the callback won't be called more often than roughly 50 to 250 times per second, even when you set the interval to 1ms. See Timer resolution in browsers (as referred to by Sani Huttunen) for an explanation.

With 1000ms it will work better. But still the timer won't be fired when the tab is inactive, and may be delayed when the cpu is busy or another script is running on your page.

One solution is to not increment a counter, but to test how much time has actually passed since the previous call of the timer. That way, the timing remains accurate, even when the intervals have been delayed or paused inbetween.

This snippet will remember the start date, and on each timer interval, update seconds and milliseconds to the difference between the current time and the start time.

var start = new Date();
var milliseconds = 0;
var seconds = 0;
setInterval(function()
{
    var now = new Date();
    milliseconds = now.getTime() - start.getTime();
    seconds = round(milliseconds / 1000);
}, 1000);

I've set the interval to 1000 again. You might set it shorter, but it will cost more performance.

Related question: How can I make setInterval also work when a tab is inactive in Chrome?

like image 52
GolezTrol Avatar answered Aug 24 '26 19:08

GolezTrol