Is there a way to pause a long polling script when someone leaves a page up but is not viewing it? So if I have multiple tabs or windows of the app open only the one I am actively viewing would have the active long polling script running?
Actually there is no efficient way to pause script in javascript. But let me suggest one:
function pausecomp(millis){
var date = new Date();
var curDate = null;
do{
curDate = new Date();
}while(curDate-date < millis);
}
So this would pause the whole script for a number of milliseconds. However this isn't a good practice.
Javascript allows to setup events to occur after a delay:
setTimeout("alert('hello')",1250);
So when this line of code is reached the setTimeout method calls the alert when 1250 milliseconds are passed.
I hope this information helps you ;)
To detect when mouse leaves the window I have setup a jsfiddle for you: http://jsfiddle.net/xPAwu/1/
Besides there are actually some questions on that on stackoverflow: How can I detect when the mouse leaves the window?
Javascript event when mouse leaves browser window
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With