Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery onscreen run offscreen pause

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?

like image 389
David Avatar asked Apr 20 '26 19:04

David


1 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?

Javascript event when mouse leaves browser window

like image 72
UpCat Avatar answered Apr 22 '26 07:04

UpCat