开发者

Multiple timers on the same page don't work smoothly

开发者 https://www.devze.com 2023-03-16 19:20 出处:网络
I have about 3 JavaScript timers working at the same time on a page using settimeout,开发者_如何学Go but the functions called by these timers are not working smoothly and it seems that the timers are

I have about 3 JavaScript timers working at the same time on a page using settimeout,开发者_如何学Go 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消