Advertisement
HyperKombu

Autorefresh

Jan 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. var refresh_rate = 5; //<-- In seconds, change to your needs
  2. var last_user_action = 0;
  3. var has_focus = false;
  4. var lost_focus_count = 0;
  5. var focus_margin = 10; // If we lose focus more then the margin we want to refresh
  6.  
  7.  
  8. function reset() {
  9. last_user_action = 0;
  10. console.log("Reset");
  11. }
  12.  
  13. function windowHasFocus() {
  14. has_focus = true;
  15. }
  16.  
  17. function windowLostFocus() {
  18. has_focus = false;
  19. lost_focus_count++;
  20. console.log(lost_focus_count + " <~ Lost Focus");
  21. }
  22.  
  23. setInterval(function () {
  24. last_user_action++;
  25. refreshCheck();
  26. }, 1000);
  27.  
  28. function refreshCheck() {
  29. var focus = window.onfocus;
  30. if ((last_user_action >= refresh_rate && !has_focus && document.readyState == "complete") || lost_focus_count > focus_margin) {
  31. window.location.reload(); // If this is called no reset is needed
  32. reset(); // We want to reset just to make sure the location reload is not called.
  33. }
  34.  
  35. }
  36. window.addEventListener("focus", windowHasFocus, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement