ãã¼ã¸ã®è¡¨ç¤ºã»é表示ç¶æ ãåå¾ããPage Visibility API
HTML5 Advent Calendarã®1æ¥ç®ã§ãã
æ¬æ¥ã®Advent Calendarã¨ã¯ã12æ1æ¥ããã¯ãªã¹ãã¹ã®25æ¥ã¾ã§ãã«ã¼ãã«ä½ãããçªã1æ¥ã«1ã¤ãã¤éãã¦ããã¨ãããã®ã§ããä¸æ¹ãæè¡ç³»ã®Advent Calendarã¯ã12æ1æ¥ãã25æ¥ã¾ã§ã®éãæ¯æ¥éã人ãç¹å®ã®ãã¼ãã«æ²¿ã£ã¦ããã°è¨äºãæ¸ãã¨ãããã®ã§ããããã§ã¯ããHTML5ãããã¼ãã«ãªãã¾ããä»ã«ãé¢ç½ãè¨äºãå
¬éãããäºå®ã§ãã®ã§èå³ã®ããæ¹ã¯æ¯éãã§ãã¯ãã¦ã¿ã¦ãã ããã
ç§ã¯ãã¨ãããã1æ¥ç®ãªã®ã§è»½ããå ãPage Visibility APIã¨ãããã¤ãã¼ãªä»æ§ã«ã¤ãã¦è§£èª¬ãããã¨æãã¾ãï¼
Page Visibility APIã¨ã¯
Page Visibility APIã¯ããã¼ã¸ã®è¡¨ç¤ºç¶æ
ãåå¾ããAPIã§ããdocument.hidden ã®ããããã£ãã表示ç¶æ
(false)ãé表示ç¶æ
(true)ã確èªãããã¨ãã§ãã¾ãã以ä¸ã®å ´åã«é表示ç¶æ
ã«ãªãã¾ãã
- ã¦ã£ã³ãã¦ãæå°åãããå ´å
- åãã¦ã£ã³ãã¦ã®ä»ã®ã¿ããè¦ã¦ããå ´å
- OSã®ããã¯ç»é¢ä¸ã®å ´åï¼Chrome15ã§ç¢ºèªããã¨ããç¶æ ã¯å¤ãããï¼
ã©ããªã¨ãã«ä½¿ããã¨è¨ãã¨ã
ä¾ãã°ãã¡ã¼ã«ãå®æçã«ãã§ãã¯ããå¦çãããã¨ãã¦ã表示ç¶æ
ã®ã¨ãã¯é »ç¹ã«ãã§ãã¯ããé表示ç¶æ
ã®ã¨ãã«ã¯ãã¾ã«ãã§ãã¯ããã®ã¿ã«ãããããªå ´åã«å©ç¨ã§ãã¾ãããããã¯ãé表示ç¶æ
ã®ã¨ãã¯ä¸ååããå¿
è¦ããªããã®ãããããããã¾ããã
表示ã»é表示ç¶æ
ã®å¤æ´ã®æ¤ç¥ã¯ãvisibilitychangeã¤ãã³ãã使ãã¾ãã以ä¸ã¯ãChromeã§ã®ãµã³ãã«ã³ã¼ãã§ãã
// for chrome document.addEventListener('webkitvisibilitychange', function(){ if ( document.webkitHidden ) { // é表示ç¶æ } else { // 表示ç¶æ } }, false);
é常ã«åç´ã§ç°¡åã§ããï¼ã¡ãªã¿ã«æè¿ã®ãã©ã¦ã¶ã¯ãããã¯ã°ã©ã¦ã³ãã¿ãã®1ç§ä»¥ä¸ã®ééã®setTimeoutãsetIntervalã«ä½ãããªãã¦ãèªåçã«é
延ãæããã®ã§æ³¨æã§ããï¼è©³ç´°ã¯ãモダンブラウザのタイマー処理の制限をWebSocketを使って突破する(WebWorkersについて追記あり)ï¼
2011å¹´12æ1æ¥ç¾å¨ã§ã¯ã以ä¸ã®å¯¾å¿ãã©ã¦ã¶ã§ãã³ãã¼ããªãã£ãã¯ã¹ä»ãã§ã¢ã¯ã»ã¹ã§ãã¾ãã
対å¿ãã©ã¦ã¶ | ||
---|---|---|
Chrome 13+ | Firefox 10 | IE 10 |
ãã¢
æ¢ã«ç§é¸ãªãã¢ããã£ãã®ã§ããããç´¹ä»ãã¾ãã:p
Internet Explorer 10 Test Drive - Page Visibility API -
表示ã»é表示ã®ç¶æ
ãã¿ã¤ã ã©ã¤ã³ã§è¡¨ç¤ºãããã¢ã§ãã
http://www.samdutton.com/pageVisibility/
Using the Page Visibility API
é表示ã®ç¶æ
ã®ã¨ãã«åç»ã®åçãã¹ãããããå表示ãããéã«åçããªã¹ã¿ã¼ããããã¢ã§ããã¿ãã®ã¢ã¤ã³ã³ã§åçãã¼ã¯ãä¸æåæ¢ãã¼ã¯ãã¤ãã®ãé¢ç½ãã§ãã
http://www.samdutton.com/pageVisibility/
APIãªãã¡ã¬ã³ã¹
Properties
ãããã㣠| 説æ |
---|---|
document.hidden | ãã¼ã¸è¡¨ç¤ºç¶æ ï¼false: 表示ãtrue: éè¡¨ç¤ºï¼ |
document.visiblityState | ãã¼ã¸è¡¨ç¤ºç¶æ
ã®è©³ç´°ï¼PAGE_HIDDEN("hidden"): é表示ãPAGE_VISIBLE("visible"): 表示ãPAGE_PRERENDER("prerender"): ãã¼ã¸ã®ã¬ã³ããªã³ã°å®äºåï¼ |
Events
ã¤ãã³ã | 説æ |
---|---|
visibilitychange | ãã¼ã¸ã®è¡¨ç¤ºç¶æ ãå¤åããéã«çºçãã |
W3Cã®ä»æ§æ¸ã¯ãã¡ãã
W3C Working Draft - Page Visibility -
http://www.w3.org/TR/2011/WD-page-visibility-20110602/
ã¾ã¨ã
ç¹ã«é¢ç½ããã¨ãæ¸ãã¦ããããã§ã¯ãªãã§ãããã¨ããããããããªAPIãããããã¨ãããã¨ã§ãã¤ãã¼ãªPage Visibility APIãç´¹ä»ãã¦ã¿ã¾ãããHTML5ã¯ãä»ã«ãç´°ããä»æ§ãã©ãã©ã追å ããã¦ãã¦è¿½ã£ãããã®ã大å¤ã§ãããã¾ãæ©ä¼ãããã°ä»ã®APIãç´¹ä»ãã¦ããããã¨æãã¾ãï¼