JQuery on screen start pause splash screen

Is there a way to pause a long polling script when someone leaves the page up but does not view it? So, if I have several tabs or windows of the application, only the one that I am actively browsing will have an active long polling script?

+3
source share
3 answers

There is really no effective way to pause a 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 will stop the whole script for a few milliseconds. However, this is not a good practice.

Javascript allows you to set events after a delay:

setTimeout("alert('hello')",1250);

So, when this line of code is reached, the setTimeout method raises a warning when 1250 milliseconds are passed.

, ;)


, , jsfiddle: http://jsfiddle.net/xPAwu/1/

, stackoverflow: , ?

Javascript,

+1

, . idleTimer , , , . (mousemove keypress ..), , .

0

, :

<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 do is capture when interacting with the user (moving the mouse or pressing a key). If this has not happened for a long time (60 seconds in this example), we stop executing our polling script.

Hope this helps

0
source

All Articles