robcowie / jquery-stopwatch

A jQuery plugin that renders a count-up clock from a defined start time
http://robcowie.github.com/jquery-stopwatch/
87 stars 31 forks source link

Update Interval #6

Open Blacksus opened 12 years ago

Blacksus commented 12 years ago

Hello Rob,

I'm using your plugin for a long time, and it's so nice! But I found a problem that I couldn't understand well. Sometimes, in some computers only, the timer goes crazy and start to count 2x faster, and sometimes jumping 1 second (counting 2 by 2). I just don't know what to do, it's strange and I can't describe what happens... =/ There's nothing complicated in the code, just this:

$("#timer-atend").stopwatch({ startTime: parseInt(timeInitial, 10), format: "{HH}:{MM}" }).stopwatch('start');

Any idea what's going on?

Thank you so much Rob, Have a nice day =]

robcowie commented 12 years ago

This is possibly due to the way setInterval() works. It will execute the callback every x seconds regardless of the successful completion of the previous invocation. It could therefore 'trip over' itself.

One possible fix is to replace setInterval() with recursive setTimeout() calls. This will stop the timer tripping-over itself and appearing to tick over faster than it should but it will potentially cause it to tick over more slowly than 'real time', i.e. 1 second may be displayed, but 1.5 seconds may have actually elapsed.

I'll have a think about it. For my info, what browser and OS was the machine using? Is the machine old? under heavy load?

Blacksus commented 12 years ago

I'll try to change the function so, calling setTimeout()

I guess the browser isn't important, since the problem occurred in all browsers installed (Chrome, Firefox, Opera and IE), all with their lasted update. The machine isn't old (usually core i5 or i3, with 8GB RAM) and usually not under heavy load (some machines was under heavy load, but another machines no, with just IE opened)

Thank you