Skip to content Skip to sidebar Skip to footer

How To Stop Function On Blur()

myfucntion = send request after 5 sec document.ready(myfucntion) $(window).focus(myfucntion) $(window).blur(stop(myfucntion)) is this possible to stop a function on blur called pr

Solution 1:

Have a global timer:

var intervalId;

And it would be better to have two functions:

startfunction() {
  intervalId = setTimeout(function () {
    // send request after 5 seconds
  }, 5000);
}
stopfunction() {
  clearTimeout(intervalId);
}

And use them like this:

$(document).ready(startfunction);
$(document).ready(function () {
  $(window).focus(startfunction);
  $(window).blur(stopfunction);
});

Solution 2:

Here is an example of how to make what you want work

var timer; // make it global so it can be accessed inside functionsvar myFunction = function () {
  timer = setTimeout(function() {
    //do something after 5 seconds
  }, 5000);
}

var stopMyFunction = function () {
  clearTimeout(timer);
}

$(document).ready(myFunction)
$(window).focus(myFunction)
$(window).blur(stopMyFunction))

Post a Comment for "How To Stop Function On Blur()"