javascript reload page every 5 seconds when browser is minimized
Asked Answered
A

1

1

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 , but in Chrome this doesn't work.

Scenario:

Minimize the browser and leave the computer idle.

I have tried two methods:

Method 1:

   <meta http-equiv="refresh" content="5;URL=http://example.com/" />

Method 2:

 <script>

   $(document).ready(function () {

   setTimeout(function(){
    window.location.reload(1);
    }, 5000);

    });

 </script>

Any help would be great.

Armor answered 14/7, 2016 at 13:54 Comment(4)
You can't identify from client javascript that the browser window is minimised.Hic
@Vision , But auto reload works in Firefox even when browser window minimised, why not in Chrome ?Armor
Why do you reload the whole page when you can just get the updated data itself from the server? Anyway wouldn't window.location.href = window.location.href; work?Benefit
Chrome behaves differently with timed events when the browser does not have focus. I haven't tried this with Firefox on the desktop, but both Firefox and Chrome on a mobile device stop firing multiple timed events.Marga
H
1

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.

Hiramhirasuna answered 14/7, 2016 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.