PageVisibility API

0
0
<!DOCTYPE HTML>  
 <html>  
 <head>  
        
           timer = 0;  
           function onLoad(){  
                document.addEventListener("visibilitychange",stateChanged);  
                document.addEventListener("webkitvisibilitychange", stateChanged);  
                document.addEventListener("msvisibilitychange", stateChanged);  
           }  
           function stateChanged(){  
                console.log(document.webkitVisibilityState);  
                if(document.hidden || document.webkitHidden || document.msHidden){  
                     //new tab or window minimized
                     timer = new Date().getTime();  
                }  
                else {  
                     alert('You were away for ' + (new Date().getTime()-timer)/1000+ ' seconds.')  
                }  
           }  
        
 </head>  
 <body onLoad="onLoad()">  
 </body>  
 </html>

Demo by Sagar Ganatra

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.