Amr Elgarhy
Amr Elgarhy

Reputation: 68912

Multiple timers on the same page don't work smoothly

I have about 3 JavaScript timers working at the same time on a page using settimeout, but the functions called by these timers are not working smoothly and it seems that the timers are waiting on each other, and some times hang the page.

Is there a best practice when dealing with js timers to avoid this behavior?

Examples:

Sometimes use jQuery timers like this:

$(document).everyTime(1000, "blinkRed", function (i) {
      $(price).toggleClass('valuered');
}, 10);

and sometimes I use the normal settimeout JavaScript function.

Upvotes: 2

Views: 499

Answers (1)

GolezTrol
GolezTrol

Reputation: 116110

The timers are not waiting for each other, but the functions that are called when the timer is done, are. So, when you put blocking code (like an alert) in the first timer function, it will prevent the other timers to go off.

Upvotes: 3

Related Questions