javascript reload page every 5 seconds when browser is minimized

前端 未结 1 2003
独厮守ぢ
独厮守ぢ 2021-01-25 12:36

Actually we Play a notification sound when some update happens in database, so I am trying to reload page every 5 seconds when browser in minimized, this works fine in Firefox ,

相关标签:
1条回答
  • 2021-01-25 13:15

    The window blur and focus events can detect the view state of the window .

    Example :

     var timer = null;
    
     //when the window is minimized or when user is in different tab . 
        window.addEventListener('blur', function(){
    
           timer = setInterval(function(){
    
              window.location.reload(1);
    
           },5000)
    
        }, false);
    
    
    //when user is back to window
        window.addEventListener('focus', function(){
    
            //stop the page reload once 
    
            if(timer != null){
    
            clearInterval(timer);
    
          }
        }, false);
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题