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?
-
This can be broken down into 2 subquestions, 1) Is it possible to detect when a user is no longer viewing a page (and, btw, what is 'viewing'), and 2) Can I use this information to pause a long-polling script (which would vary based on your long polling solution) – Matt May 02 '11 at 16:17
-
Related: [Is there a way to detect if a browser window is not currently active](http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active) – Erik May 02 '11 at 16:45
-
viewing means does not have focus on that particular window/tab. Long polling wise, I can stop it myself easy if I just have something to trigger to pause/stop until focus is back on that window – David May 02 '11 at 17:57
3 Answers
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?

- 1
- 1

- 69,338
- 131
- 383
- 601
-
Your first script will hang the browser. It's dangerous to advice it – Edgar Villegas Alvarado May 02 '11 at 17:27
-
I pointed out that he should not use it, I wrote it to point out the contrast – DarkLeafyGreen May 02 '11 at 17:35
The difficulty here is knowing if the user is actually idle or not. I have used this idleTimer plugin in the past and it seemed to work out pretty well. The author uses a combination of events (mousemove keypress, etc) to decide if the user is active or not.

- 29,697
- 8
- 65
- 67
You could see how much time the page is idle:
<script>
$(document).ready(function(){
var lastEventTime = null;
//Detect a user interaction
$(document).bind("mousemove keyup mousewheel", function(event){
lastEventTime = new Date(); //Store last event time
});
setInterval(function(){
var currentTime = new Date();
var idleSeconds = (currentTime.getTime() - lastEventTime.getTime())/1000;
if( idleSeconds > 60) return; //If more than 60 seconds idle, stop polling
pollingScript(); //Here goes your script
}, 1000);
});
</script>
What we're doing, is capture when there's a user interaction (move mouse or press a key). If it didn't happen for a long time (60 seconds in this example), we stop executing our polling script.
Hope this helps

- 18,204
- 2
- 42
- 61