jquery - Detect the browser tab switching/ browser minimising using javascript -


i trying code own versions of popular games such minesweeper, snake etc using javascript. of games require timer, wonder possible detect whether user switched tab or minimized browser can put game on pause mode? provided!

you set var when window catches onblur event.

<script> var has_blurred = 0; function meep() {     has_blurred = 1;     game.pause(); } window.onblur=meep; </script> 

edit adding onfocus

then later on in same window/tab, can handle if window/tab has ever blurred onfocus handler.

<script> function handlefocus() {     if( has_blurred )         game.unpause();                   has_blurred = 0; // reset has_blurred state } window.onfocus=handlefocus; </script> 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -