After a few cycles, the timer begins to slow down, what could be the reason? Initially, I do so that after a certain period of time, pictures appear in one or another row, but after several cycles the timer is lost. What could be the reason ?

Here is the code:

var fn = function() { function animation_zone() { tgl++; if (tgl == 1) { $("div").fadeOut("0", function() { $("div").fadeOut("1000", function() { $("div").fadeIn("1000"); }); }); $("div").fadeOut("1000", function() { $("div").fadeIn("1000"); setTimeout(function() { $("div").fadeIn("1000", function() {}) }, 7000) }); } if (tgl == 2) { $("div").stop().fadeOut("0", function() { $("div").fadeOut("1000", function() { $("div").fadeIn("1000"); }); }); $("div").fadeOut("1000", function() { $("div").fadeIn("1000"); }); setTimeout(function() { $("div").fadeIn("1000"); }, 7000) tgl = 0; } } animation_zone(); setTimeout(arguments.callee, 14300); } setTimeout(fn, 14300); 

    1 answer 1

    There are many factors due to which there can be a significant error per millisecond, which can become noticeable over time. For example, the situations below can significantly affect:

    Actual frequency of operation

    In some situations, the timer will operate less frequently than usual. The delay between calls to setInterval (..., 4) may not be 4 ms, but 30 ms, or even 1000 ms.

    • Most browsers (desktop first) continue to execute setTimeout / setInterval, even if the tab is inactive.

    • At the same time, a number of them (Chrome, FF, IE10) reduce the minimum frequency of the timer, to 1 time per second. It turns out that in the “background” tab the timer will work, but rarely.

    • When running on battery, in a laptop, browsers can also reduce the frequency in order to run the code less often and save battery power. IE is particularly known for this. Reduction can reach several times, depending on the settings.

    • If the processor load is too high, JavaScript may not have time to process timers on time. However, some setInterval starts will be missed.

    Learn more about the use of timers and trigger delays.
    Details by reference: setTimeout and setInterval