Answers for "Pausing setInterval when page/ browser is out of focus"

0

Pausing setInterval when page/ browser is out of focus

var myInterval;
var interval_delay = 500;
var is_interval_running = false; //Optional

$(document).ready(function () {
    $(window).focus(function () {
        clearInterval(myInterval); // Clearing interval if for some reason it has not been cleared yet
        if  (!is_interval_running) //Optional
            myInterval = setInterval(interval_function, interval_delay);
    }).blur(function () {
        clearInterval(myInterval); // Clearing interval on window blur
        is_interval_running = false; //Optional
    });
});

interval_function = function () {
     is_interval_running = true; //Optional
     // Code running while window is in focus
}
Posted by: Guest on December-31-2021

Code answers related to "Pausing setInterval when page/ browser is out of focus"

Code answers related to "Javascript"

Browse Popular Code Answers by Language