JavaScript timeouts are not guaranteed at specific times. For example, if a thread is busy with something else at the time the timer ends, it will first complete what it does and then execute your timer.
Also, your function does not take into account the time spent inside the display function, so a slight delay will be added for every millisecond.
The correct way to implement a timer is to use system time.
So something like:
//Call at the beggining to save the start time var start_time = new Date() // Compute seconds (does not matter when/how often you call it.) var milliseconds_since_start = new Date().valueOf() - start_time
The Date object can also format this period as a clock for you:
var m = new Date(milliseconds_since_start) m.getMinutes()+":"+m.getSeconds()
source share