{"id":391,"date":"2019-01-25T17:31:02","date_gmt":"2019-01-25T09:31:02","guid":{"rendered":"https:\/\/wp.winter-weng.cc\/work\/?p=391"},"modified":"2019-01-25T17:31:02","modified_gmt":"2019-01-25T09:31:02","slug":"js-page-visibility-api","status":"publish","type":"post","link":"https:\/\/wp.winter-weng.cc\/work\/?p=391","title":{"rendered":"[JS] Page Visibility API"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_75 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"\u986f\u793a\/\u96b1\u85cf\u5167\u5bb9\u76ee\u9304\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=391\/#visibilityState_%E5%B1%AC%E6%80%A7\" >visibilityState \u5c6c\u6027<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=391\/#visibilitychange_%E4%BA%8B%E4%BB%B6\" >visibilitychange \u4e8b\u4ef6<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=391\/#%E5%AF%A6%E5%8B%99\" >\u5be6\u52d9<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=391\/#%E5%8F%83%E8%80%83%E8%B3%87%E6%96%99\" >\u53c3\u8003\u8cc7\u6599<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>\u6700\u8fd1\u5728\u505a\u624b\u6a5f\u7248\u9801\u9762\uff0c\u7576\u96e2\u958b\u6642\u9700\u8981\u7d00\u9304\u6700\u5f8c\u7684\u72c0\u614b\uff0c\u4e00\u822c\u4f86\u8aaa\u6700\u76f4\u89ba\u6703\u60f3\u5230\u76e3\u807d beforeunload \u548c unload \u5169\u500b\u4e8b\u4ef6\uff0c\u7136\u800c\u6e2c\u8a66\u767c\u73fe\u9019\u5169\u500b\u4e8b\u4ef6\u5728\u95dc\u9589\u9801\u7c64\u548c\u56de\u5230\u684c\u9762\u6642\u4e0d\u6703\u89f8\u767c\uff0c\u53cd\u800c\u662f\u91cd\u65b0\u6574\u7406\u624d\u6703\u89f8\u767c\u3002<br \/>Testing on:<br \/>Chrome 71.0.3578 @ iPhone XS (iOS 12.0.1)<br \/>Safari 12.0 @ iPhone XS (iOS 12.0.1)<br \/>Chrome 71.0.3578 @ Sony XZ (Android 8.0.0)<br \/><\/p>\n\n\n<p>\u800c\u4e14\u5728\u624b\u6a5f\u4e0a\uff0c\u4f7f\u7528\u8005\u4e5f\u53ef\u80fd\u76f4\u63a5\u56de\u5230\u684c\u9762\u5f8c\u900f\u904e\u61c9\u7528\u7a0b\u5f0f\u7ba1\u7406\u54e1\u95dc\u9589\u700f\u89bd\u5668\uff0c\u5982\u6b64\u4e00\u4f86\u5c31\u7121\u6cd5\u6e96\u78ba\u7d00\u9304\u5230\u4f7f\u7528\u8005\u7684\u884c\u70ba\u3002\u70ba\u4e86\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0cW3C \u63d0\u51fa\u4e86 <a href=\"https:\/\/www.w3.org\/TR\/page-visibility-2\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Page Visibility API (\u5728\u65b0\u5206\u9801\u4e2d\u958b\u555f)\">Page Visibility API<\/a>\uff0c\u900f\u904e\u6b64 API\uff0c\u53ef\u4ee5\u76e3\u807d\u5230\u9801\u9762\u7684\u53ef\u898b\u6027\u8b8a\u5316\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"visibilityState_%E5%B1%AC%E6%80%A7\"><\/span>visibilityState \u5c6c\u6027<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>API \u5728 <code>document<\/code> \u589e\u52a0\u4e86 <code>document.visibilityState<\/code> \u5c6c\u6027\uff0c\u56de\u50b3\u4e09\u500b\u503c\u4f86\u8868\u793a\u9801\u9762\u53ef\u898b\u7684\u72c0\u614b\uff1a<\/p>\n\n\n<ul class=\"wp-block-list\"><li>hidden\uff1a\u9801\u9762\u4e0d\u53ef\u898b<\/li><li>visible\uff1a\u9801\u9762\u81f3\u5c11\u90e8\u5206\u53ef\u898b<\/li><li>prerender\uff1a\u9801\u9762\u5373\u5c07\u6216\u6b63\u5728\u6e32\u67d3\uff0c\u8655\u65bc\u4e0d\u53ef\u898b\u72c0\u614b\uff08\u5982 chrome \u700f\u89bd\u5668\u5728\u4e0d\u53ef\u898b\u72c0\u614b\u4e0b\uff0c\u6703\u5148\u5c07\u9801\u9762\u9810\u5148\u6e32\u67d3\u51fa\u4f86\uff0c\u7b49\u7528\u6236\u700f\u89bd\u7684\u6642\u5019\u518d\u76f4\u63a5\u5c55\u793a\u3002\uff09<\/li><\/ul>\n\n\n<p>\u5728\u4ee5\u4e0b\u60c5\u6cc1\u9801\u9762\u6703\u662f <code>hidden<\/code> \uff1a<\/p>\n\n\n<ul class=\"wp-block-list\"><li>\u700f\u89bd\u5668\u6700\u5c0f\u5316<\/li><li>\u700f\u89bd\u5668\u88ab\u5207\u63db\u5230\u80cc\u666f<\/li><li>\u700f\u89bd\u5668\u5c07\u8981\u5378\u8f09 (unload)<\/li><li>\u64cd\u4f5c\u7cfb\u7d71\u89f8\u767c\u87a2\u5e55\u9396\u5b9a<\/li><\/ul>\n\n\n<p>\u5982\u6b64\u4e00\u4f86\uff0c\u6211\u5011\u5c31\u80fd\u5920\u5728\u9801\u9762\u4e0d\u53ef\u898b\u7684\u60c5\u6cc1\u4e0b\u505c\u6389\u4e00\u4e9b\u884c\u70ba\uff0c\u50cf\u662f\u5f71\u7247\u3001\u97f3\u6a94\u64ad\u653e\u3001API \u8f2a\u8a62\u3001Slider \u8a08\u6642\u7b49\u7b49\u3002<\/p>\n\n\n<p>\u57fa\u65bc\u6b77\u53f2\u539f\u56e0\uff0cAPI \u9084\u5305\u542b\u4e86 <code>document.hidden<\/code> \u5c6c\u6027\uff0c\u8fd4\u56de\u5e03\u6797\u503c\u3002\u5728\u53ef\u4ee5\u7684\u60c5\u6cc1\u4e0b\uff0c\u90fd\u5efa\u8b70\u4f7f\u7528 <code>document.visibilityState<\/code> \u5c6c\u6027\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"visibilitychange_%E4%BA%8B%E4%BB%B6\"><\/span>visibilitychange \u4e8b\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u7576 <code>document.visibilityState<\/code> \u5c6c\u6027\u767c\u751f\u8b8a\u5316\uff0c\u5c31\u6703\u89f8\u767c  <code>visibilitychange<\/code> \u4e8b\u4ef6\uff0c\u56e0\u6b64\u53ef\u4ee5\u76e3\u807d\u6b64\u4e8b\u4ef6\u4f86\u8ffd\u8e64\u9801\u9762\u53ef\u898b\u6027\u7684\u8b8a\u5316\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AF%A6%E5%8B%99\"><\/span>\u5be6\u52d9<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u7d93\u904e\u5be6\u6a5f\u6e2c\u8a66\uff0c\u5728\u4ee5\u4e0b\u60c5\u6cc1\u4e0b\u53ef\u4ee5\u6b63\u78ba\u89f8\u767c\u4e8b\u4ef6\uff0c\u4e26\u4e14 <code>document.visibilityState<\/code> \u5c6c\u6027\u70ba <code>hidden<\/code>\u3002<\/p>\n\n\n<ul class=\"wp-block-list\"><li>\u5207\u63db\u61c9\u7528\u7a0b\u5f0f<\/li><li>\u56de\u5230\u684c\u9762<\/li><\/ul>\n\n\n<p>\u800c\u300c\u95dc\u9589\u8996\u7a97\u300d\u5247\u6709\u9ede\u5fae\u5999\uff0c\u5728 Chorme 71.0.3578 @ iPhone XS (iOS 12.0.1) \u6703\u89f8\u767c\uff0c\u5176\u9918\u5169\u500b\u60c5\u6cc1 (Safari 604.1 @ iPhone XS (iOS 12.0.1)\u3001Chorme 71.0.3578 @ Sony XZ (Android 8.0.0)) \u5247\u4e0d\u6703\u3002<\/p>\n\n\n<p>\u56e0\u6b64\u5728\u6b64\u60c5\u5883\u4e2d\u8981\u6e96\u78ba\u6293\u5230\u95dc\u9589\u8996\u7a97\u9084\u662f\u6709\u9ede\u554f\u984c\uff0c\u73fe\u5728\u66ab\u6642\u9084\u6c92\u6709\u66f4\u597d\u7684\u89e3\u6cd5\uff0c\u751a\u81f3\u6709\u5728\u8003\u616e\u8981\u4e0d\u8981\u5728 <code>document<\/code> \u6216 <code>window<\/code> \u4e0a\u7d81\u5b9a <code>blur<\/code> \u4e8b\u4ef6 (XD)\u3002\u4f46\u9019\u500b API \u4ecd\u7136\u89e3\u6c7a\u4e86\u90e8\u5206\u7684\u554f\u984c\u3002<\/p>\n\n\n<p>\u7531\u65bc\u7db2\u7ad9\u8de8\u88dd\u7f6e\u7684\u7de3\u6545\uff0c\u8981\u8655\u7406\u7684\u72c0\u6cc1\u4e5f\u8d8a\u4f86\u8d8a\u591a\uff0c\u53e6\u5916\u9084\u6709 <a href=\"https:\/\/wicg.github.io\/page-lifecycle\/spec.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Page Lifecycle API (\u5728\u65b0\u5206\u9801\u4e2d\u958b\u555f)\">Page Lifecycle API<\/a> \u53ef\u4ee5\u8655\u7406\u6574\u500b\u9801\u9762\u751f\u547d\u9031\u671f\u7684\u8f49\u63db\uff0c\u524d\u7aef\u8def\u771f\u662f\u8d8a\u8d70\u8d8a\u5bec\u4e86\uff5e<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%8F%83%E8%80%83%E8%B3%87%E6%96%99\"><\/span>\u53c3\u8003\u8cc7\u6599<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" aria-label=\"Page Visibility Level 2 (\u5728\u65b0\u5206\u9801\u4e2d\u958b\u555f)\" href=\"https:\/\/www.w3.org\/TR\/page-visibility-2\/\" target=\"_blank\">[W3C] Page Visibility Level 2<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\"[W3C] Page Lifecycle 1 (\u5728\u65b0\u5206\u9801\u4e2d\u958b\u555f)\" href=\"https:\/\/wicg.github.io\/page-lifecycle\/spec.html\" target=\"_blank\">[W3C] Page Lifecycle 1<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\"[\u962e\u4e00\u5cf0] Page Visibility API \u6559\u7a0b (\u5728\u65b0\u5206\u9801\u4e2d\u958b\u555f)\" href=\"http:\/\/www.ruanyifeng.com\/blog\/2018\/10\/page_visibility_api.html\" target=\"_blank\">[\u962e\u4e00\u5cf0] Page Visibility API \u6559\u7a0b<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\"[\u962e\u4e00\u5cf0] Page Lifecycle API \u6559\u7a0b (\u5728\u65b0\u5206\u9801\u4e2d\u958b\u555f)\" href=\"http:\/\/www.ruanyifeng.com\/blog\/2018\/11\/page_lifecycle_api.html\" target=\"_blank\">[\u962e\u4e00\u5cf0] Page Lifecycle API \u6559\u7a0b<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\"[Chrome Developer] Chromium \u7684\u5f48\u7a97\u6a5f\u5236 (\u5728\u65b0\u5206\u9801\u4e2d\u958b\u555f)\" href=\"https:\/\/developers.google.com\/web\/updates\/2017\/03\/dialogs-policy?hl=zh-tw\" target=\"_blank\">[Google Developer] Chromium \u7684\u5f48\u7a97\u6a5f\u5236<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u505a\u624b\u6a5f\u7248\u9801\u9762\uff0c\u7576\u96e2\u958b\u6642\u9700\u8981\u7d00\u9304\u6700\u5f8c\u7684\u72c0\u614b\uff0c\u4e00&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[4],"tags":[21,33],"class_list":["post-391","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript","tag-33"],"_links":{"self":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=391"}],"version-history":[{"count":0,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/391\/revisions"}],"wp:attachment":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}