I need to implement the auto-refresh of the page after the PC goes out of sleep, and after unlocking the screen. I have been sitting on this task all day and can’t decide.

  • one
    Most likely, do it by indirect signs - andreymal
  • one
    And I recently wrote a script on js that opens the gate to the garage: D about the same complexity. But seriously, you need to track when the user has left the tab and when he returned. This question has already been, I remember. - Zicrael
  • window.addEventListener ('blur' window.addEventListener ('focus' will help think) - ffeynmann
  • Well then, no way :) there is no such event for sure) - Zicrael

2 answers 2

There is no such event)))
Alternatively, use the window.onoffline and window.ononline events. Thus, you can determine the state of the Internet connection.

 var sts = document.getElementById('status'), eventStatus = function(e){ console.log(e.type); sts.className = e.type; }; window.onoffline = window.ononline = eventStatus; sts.className = navigator.onLine ? 'online' : 'offline'; 
 #status { display: block; width: 20px; height: 20px; border-radius: 50%; } #status.online { background-color: green; } #status.offline { background-color: red; } 
 <div id="status"></div> 

  • Wow, there are no such events .. But for thinking in the right direction + - Zicrael
  • There are such events. Chrome, Opera, Mozilla, IE11 for sure. - Andrew ProjectSoft
  • I just google it, these are the events that you write yourself :) They depend on navigator.onLine so there are no such events, but the thoughts are good. - Zicrael
  • @Zicrael I developed the extension "Online Radio in Your Browser" and one of the functionals was to continue receiving the stream when the connection was lost. I know what I'm writing about. developer.mozilla.org/en-US/docs/Web/API/Document/ononline Would you test ...))) - Andrew ProjectSoft
  • This is good :) well, that means it is, only in the answer, change to window with document - Zicrael

There is also a visibilitychange event, as opposed to focus/blur , which is generated only in cases when the user switches a tab or minimizes the browser. Will it work when you close the lid of the laptop - check there is no possibility. Check - tell us.

 document.addEventListener("visibilitychange", chekVisibility); function chekVisibility(event){ console.log(event); console.log(document.hidden) } 
 <div></div> 

enter image description here