Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 0.92 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. In a browser is there a way to find out whether the page is currently visible?
  2. (function () {
  3.  
  4.     var requiredResolution = 10; // ms
  5.     var checkInterval = 1000; // ms
  6.     var tolerance = 20; // percent
  7.  
  8.  
  9.     var counter = 0;
  10.     var expected = checkInterval / requiredResolution;
  11.     //console.log('expected:', expected);
  12.  
  13.     window.setInterval(function () {
  14.         counter++;
  15.     }, requiredResolution);
  16.  
  17.     window.setInterval(function () {
  18.         var deviation = 100 * Math.abs(1 - counter / expected);
  19.         // console.log('is:', counter, '(off by', deviation , '%)');
  20.         if (deviation > tolerance) {
  21.             console.warn('Timer resolution not sufficient!');
  22.         }
  23.         counter = 0;
  24.     }, checkInterval);
  25.  
  26. })();
  27.        
  28. if (/*@cc_on!@*/false) { // check for Internet Explorer
  29.     document.onfocusin = onFocus;
  30.     document.onfocusout = onBlur;
  31. } else {
  32.     window.onfocus = onFocus;
  33.     window.onblur = onBlur;
  34. }