{"id":258,"date":"2022-04-21T14:55:35","date_gmt":"2022-04-21T06:55:35","guid":{"rendered":"https:\/\/www.osvlabs.com\/blog\/?p=258"},"modified":"2022-04-21T14:55:35","modified_gmt":"2022-04-21T06:55:35","slug":"%e7%94%a8requestanimationframe-%e6%9d%a5%e8%ae%a9js%e5%8a%a8%e7%94%bb%e6%9b%b4%e6%b5%81%e7%95%85","status":"publish","type":"post","link":"https:\/\/blog.osvlabs.com\/?p=258","title":{"rendered":"\u7528requestAnimationFrame \u6765\u8ba9JS\u52a8\u753b\u66f4\u6d41\u7545"},"content":{"rendered":"<h1>\u524d\u8a00<\/h1>\n<p>\u90fd 2022 \u5e74\u4e86\uff0c\u8fd8\u4e0d\u5728\u9875\u9762\u4e2d\u52a0\u70b9\u52a8\u753b\u5417\uff1f<\/p>\n<p>\u5408\u7406\u9ad8\u6548\u7684\u52a8\u753b\u4f1a\u8ba9\u7528\u6237\u611f\u89c9\u7684\u4f53\u9a8c\u4e0e\u4ea4\u4e92\u66f4\u597d\uff0c\u5728 web \u4e2d\uff0c\u5e38\u5e38\u4f7f\u7528 CSS \u52a8\u753b\uff08transition \u4e0e keyFrames\uff09\u3001SVG \u52a8\u753b\uff08SMIL, JavaScript, CSS\uff09\u3001JavaScript \u6765\u5b9e\u73b0\u52a8\u753b\u3002<\/p>\n<p>\u5176\u4e2d\uff0cJavaScript \u56e0\u4e3a\u53ef\u63a7\u5236\u7684\u7ec6\u8282\u66f4\u591a\uff0c\u53ef\u5b8c\u6210\u7684\u6548\u679c\u4e5f\u66f4\u591a\u3002<\/p>\n<p>\u6309\u7167\u65e7\u6709\u7684\u65b9\u5f0f\uff0c\u901a\u5e38\u5728\u4f7f\u7528 JavaScript \u505a\u52a8\u753b\u65f6\uff0c\u4f1a\u4f7f\u7528 setTimeout\/setInterval \u6765\u5b8c\u6210\u52a8\u753b\u6548\u679c\uff0c\u6bd4\u5982\u65e7\u7248\u672c\u7684 jQuery \u4e2d\u7684 animate \u65b9\u6cd5\uff08\u65b0\u7248\u672c\u7684\u5df2\u7ecf\u4f7f\u7528requestAnimationFrame\uff09\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u90fd 2022 \u5e74\u4e86\uff0c\u653e\u8fc7 setTimeout\/setInterval \u5427\uff0c\u5b83\u4eec\u771f\u7684\u4e0d\u9002\u5408\u505a\u8fd9\u4e2a\u3002<\/p>\n<p>\u800c<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/window\/requestAnimationFrame\">requestAnimationFrame<\/a> \u8fd9\u4e2a API\uff0c\u770b\u540d\u5b57\u5c31\u77e5\u9053\u662f\u4e3a\u4e86\u52a8\u753b\u800c\u751f\u7684\uff0c\u4f46\u5b83\u4f5c\u4e3aWeb API\uff0c\u53ea\u8981\u9700\u6c42\u7b26\u5408\uff0c\u4e00\u6837\u53ef\u4ee5\u5229\u7528\u5176\u7279\u6027\u5b8c\u6210\u6548\u679c\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u4e86\u89e3\u4e00\u4e0b\u3002<\/p>\n<h1>\u52a8\u753b\u5e27<\/h1>\n<p>\u65e0\u8bba\u7528\u4ec0\u4e48\u6765\u5b9e\u73b0\u52a8\u753b\uff0c\u8fd0\u7528\u7684\u539f\u7406\u90fd\u662f\u76f8\u8fd1\u7684\u3002<\/p>\n<p>\u4eba\u773c\u6709\u77ed\u6682\u7684\u89c6\u89c9\u6b8b\u7559\uff0c\u5373\u5f53\u4eba\u773c\u770b\u5230\u4e00\u4e2a\u56fe\u50cf\u4fe1\u606f\u540e\uff0c\u8fd9\u4e2a\u753b\u9762\u4f1a\u77ed\u6682\u7684\u505c\u7559\u5728\u89c6\u7f51\u819c\u4e0a\u4e00\u6bb5\u65f6\u95f4\u3002<\/p>\n<p>\u65e9\u671f\u7535\u5f71\u6216\u52a8\u753b\u884c\u4e1a\u5e38\u7528\u7684\u5c31\u662f 24 \u5e27\uff0c\u5373 1s \u64ad\u653e 24 \u4e2a\u4e00\u8fde\u4e32\u7684\u753b\u9762\uff0c\u6bcf\u4e00\u4e2a\u753b\u9762\u5c31\u662f 1 \u5e27\u3002\u4f46\u5b9e\u9645\u6765\u8bf4\uff0c\u5e27\u7387\uff08FPS\uff09\u8d8a\u9ad8\uff0c\u5b9e\u9645\u611f\u89c9\u5c31\u4f1a\u8d8a\u6d41\u7545\u3002<\/p>\n<p>\u5728 web \u4e0a\u5e76\u6ca1\u6709\u4ec0\u4e48\u4e0d\u540c\uff0cweb \u52a8\u8d77\u6765\u6240\u8981\u7684\u5e27\uff08\u753b\u9762\uff09\u4e5f\u662f\u76f8\u8fd1\u7684\uff0c\u4f46\u5728 web \u4e0a\u901a\u5e38\u4e0d\u4f1a\u9009\u62e9 24 \u4f5c\u4e3a\u5e27\u7387\uff0c\u800c\u662f\u91c7\u7528\u6d4f\u89c8\u5668\u7684\u5237\u65b0\u7387\u6765\u4f5c\u4e3a\u5e27\u7387\u3002<\/p>\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\u8fd9\u4e00\u503c\u662f 60 \u5e27\uff0c\u5373\u6211\u4eec\u9700\u8981\u5728 1000\/60\u224816.7ms \u5185\u5b8c\u6210\u4e00\u5e27\u7684\u7ed8\u5236\u3002<\/p>\n<h1>\u4e3a\u4ec0\u4e48\u4e0d\u8981\u7528 setTimeout\/setInterval \u6765\u5b8c\u6210\u52a8\u753b<\/h1>\n<p>\u6709\u4e86\u65f6\u95f4\uff0c\u6709\u4e86\u8981\u505a\u7684\u76ee\u6807\uff08\u7ed8\u5236\u65b0\u753b\u9762\uff0c\u901a\u5e38\u6765\u8bf4\u5c31\u662f\u64cd\u4f5c\u5143\u7d20\uff09\uff0c\u5f88\u5bb9\u6613\u5c31\u4f1a\u60f3\u5230\u4f7f\u7528<a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/setTimeout\">setTimeout\/setInterval<\/a> API \u6765\u505a\u3002<\/p>\n<p>\u4f46\u6700\u5927\u6700\u5927\u7684\u95ee\u9898\u662f\uff0csetTimeout\/setInterval \u7684 delay \u503c\u5e76\u4e0d\u7cbe\u786e\uff0c\u5b83\u5e76\u4e0d\u80fd\u4fdd\u8bc1\u4ee3\u7801\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u53bb\u6267\u884c\u4ee3\u7801\uff0cMDN \u751a\u81f3\u76f4\u63a5\u5c06\u5176\u5199\u8fdb\u4e86\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<blockquote>\n<p>\u4e0d\u7ba1\u662f\u54ea\u79cd\u60c5\u51b5\uff0c\u5b9e\u9645\u7684\u5ef6\u8fdf\u65f6\u95f4\u53ef\u80fd\u4f1a\u6bd4\u671f\u5f85\u7684(delay \u6beb\u79d2\u6570) \u503c\u957f<\/p>\n<\/blockquote>\n<p>\u8fd9\u5bf9\u4e8e\u52a8\u753b\u6765\u8bf4\u662f\u81f4\u547d\u7684\uff0c\u8089\u773c\u5f88\u5bb9\u6613\u611f\u77e5\u51fa\u8fd9\u79cd\u201c\u4e0d\u6d41\u7545\u201d\u3002<\/p>\n<p>\u5177\u4f53\u7684\u539f\u56e0\u9700\u8981\u4e86\u89e3\u6d4f\u89c8\u5668\u7684 Event-loop \u673a\u5236\u4e0e DelayTask\uff0c\u4f46\u4e0d\u7ba1\u600e\u6837\uff0c\u5b83\u4eec\u6267\u884c\u7684\u65f6\u673a\u90fd\u662f\u4e0d\u51c6\u786e\u7684\u3002<\/p>\n<p>\u90a3\u4e48\u628a delay \u503c\u6539\u5c0f\u4e00\u4e9b\u53ef\u4ee5\u5417\uff1f<\/p>\n<p>\u5f53\u7136\u4e5f\u662f\u4e0d\u884c\u7684\uff0c\u4f8b\u5982\u8bbe\u4e3a 10ms \u66f4\u65b0\u753b\u9762\uff0c\u8fd9\u6837\u5728\u5b9e\u9645\u663e\u793a\u65f6\u5fc5\u5b9a\u4f1a\u5ffd\u7565\u67d0\u753b\u9762\uff0c\u9020\u6210\u8df3\u5e27\uff08\u67d0\u753b\u9762\u8089\u773c\u5e76\u6ca1\u6709\u770b\u5230\uff09\u3002<\/p>\n<p>\u90a3\u4e48\u6709\u6ca1\u6709\u53ef\u4ee5\u51c6\u786e\u6267\u884c\u66f4\u65b0\u753b\u9762\u7684 API \u5462\uff1f\u5f53\u5f53\u5f53\u5f53\u5f53\uff0c\u4e3b\u89d2 requestAnimationFrame \u51fa\u9a6c\u4e86\u3002<\/p>\n<h1>\u4e3a\u4ec0\u4e48\u7528 requestAnimationFrame \u66f4\u597d<\/h1>\n<h2>\u4e0e\u5237\u65b0\u7387\u543b\u5408\u7684\u65f6\u95f4<\/h2>\n<p>setTimeout\/setInterval \u7684\u65f6\u95f4\u4e0d\u51c6\u786e\uff0c\u800c requestAnimationFrame \u7684\u65f6\u95f4\u53ef\u4ee5\u8bf4\u975e\u5e38\u51c6\u786e\u3002<\/p>\n<p>\u5b83\u6267\u884c\u7684\u65f6\u673a\u5b8c\u5168\u53d6\u51b3\u4e8e\u6d4f\u89c8\u5668\u91cd\u7ed8\u7684\u65f6\u673a\uff0c\u5b83\u7684\u56de\u8c03\u51fd\u6570\u4f1a\u5728\u4e0b\u6b21\u6d4f\u89c8\u5668\u91cd\u7ed8\u4e4b\u524d\u6267\u884c\u3002<\/p>\n<p>\u5927\u90e8\u5206\u7684\u6d4f\u89c8\u5668\u5237\u65b0\u7387\u90fd\u662f 60hz, \u4f46\u8fd1\u4e9b\u5e74\u6765\u9ad8\u5237\uff0875hz, 90hz, 120hz, 144hz, 240hz)\u663e\u793a\u8bbe\u5907\u5728\u5feb\u901f\u666e\u53ca\uff0crequestAnimationFrame \u4f1a\u6839\u636e\u663e\u793a\u8bbe\u5907\u7684\u5237\u65b0\u7387\u6765\u8fd0\u884c\uff0c\u53ef\u4ee5\u770b\u5982\u4e0b\u6d4b\u8bd5\uff1a<\/p>\n<p><iframe height=\"300\" style=\"width: 100%;\" scrolling=\"no\" title=\"requestAnimationFrameTime\" src=\"https:\/\/codepen.io\/hnsxxscyx\/embed\/RwxBbQq?default-tab=html%2Cresult\" frameborder=\"no\" loading=\"lazy\" allowtransparency=\"true\" allowfullscreen=\"true\"><br \/>\n  See the Pen <a href=\"https:\/\/codepen.io\/hnsxxscyx\/pen\/RwxBbQq\"><br \/>\n  requestAnimationFrameTime<\/a> by \u4e0d\u5f97\u4e0d\u9519 (<a href=\"https:\/\/codepen.io\/hnsxxscyx\">@hnsxxscyx<\/a>)<br \/>\n  on <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n<p>\u6ce8\u610f requestAnimationFrame \u7684\u56de\u8c03\u4e2d\u4f1a\u6709\u4e00\u4e2a\u53c2\u6570\uff0c\u5b83\u662f\u4e00\u4e2a<a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/DOMHighResTimeStamp\">DOMHighResTimeStamp<\/a> \u7c7b\u578b\u503c\uff0c\u6240\u4ee5\u65f6\u95f4\u5dee\u53ef\u4ee5\u5230\u5c0f\u6570\u4f4d\u3002<\/p>\n<p>\u5f53 startLog \u65f6\uff0c\u5b9e\u6d4b\u5728 60hz \u7684\u663e\u793a\u8bbe\u5907\u4e0b\uff0clog \u7684\u503c\u901a\u5e38\u662f 16.683 \u5de6\u53f3\uff0c\u5728 120hz \u7684\u663e\u793a\u8bbe\u5907\u4e0b\uff0clog \u7684\u503c\u901a\u5e38\u4e3a 8.3 \u5de6\u53f3\uff0c\u65f6\u95f4\u503c\u57fa\u672c\u90fd\u4e0e\u5237\u65b0\u7387\u6240\u543b\u5408\u3002<\/p>\n<h2>\u907f\u514d\u65e0\u6548\u7684\u8d44\u6e90\u6d6a\u8d39<\/h2>\n<p>requestAnimationFrame \u8fd0\u884c\u5728\u91cd\u7ed8\u4e4b\u524d\uff0c\u6240\u4ee5\u5b83\u4f1a\u5c06\u6bcf\u4e00\u5e27\u7684 DOM \u64cd\u4f5c\u96c6\u4e2d\u8d77\u6765\uff0c\u5728\u4e00\u6b21\u4e2d\u56de\u6d41\u6216\u91cd\u7ed8\u4e2d\u5b8c\u6210\u3002\u8fd9\u610f\u5473\u7740\u5373\u4f7f\u5728\u4e0d\u540c\u4f4d\u7f6e\u540c\u65f6\u89e6\u53d1\u4e86\u591a\u6b21 requestAnimationFrameTime \u56de\u8c03\uff0c\u66f4\u65b0\u4e5f\u53ea\u4f1a\u5728\u4e00\u5e27\u4e2d\u8fdb\u884c\u3002\u8fd9\u4e00\u70b9\u4e5f\u53ef\u4ee5\u5728\u56de\u8c03\u51fd\u6570\u7684\u53c2\u6570\u4e2d\u5f97\u4ee5\u9a8c\u8bc1\uff1a<\/p>\n<blockquote>\n<p>\u5728\u540c\u4e00\u4e2a\u5e27\u4e2d\u7684\u591a\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5b83\u4eec\u6bcf\u4e00\u4e2a\u90fd\u4f1a\u63a5\u53d7\u5230\u4e00\u4e2a\u76f8\u540c\u7684\u65f6\u95f4\u6233\uff0c\u5373\u4f7f\u5728\u8ba1\u7b97\u4e0a\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u7684\u5de5\u4f5c\u8d1f\u8f7d\u671f\u95f4\u5df2\u7ecf\u6d88\u8017\u4e86\u4e00\u4e9b\u65f6\u95f4\u3002<\/p>\n<\/blockquote>\n<p>\u5f53\u9875\u9762\u4e0d\u662f\u6fc0\u6d3b\u72b6\u6001\u65f6\uff0crequestAnimationFrame \u4f1a\u81ea\u52a8\u505c\u6b62\uff0c\u4e5f\u4f1a\u8282\u7701\u8d44\u6e90\u5f00\u9500\u3002<\/p>\n<h1>requestAnimationFrame \u7684\u5176\u4ed6\u7528\u5904<\/h1>\n<h2>\u7ed9\u4e8b\u4ef6\u6dfb\u52a0\u8282\u6d41\u6216\u9632\u6296<\/h2>\n<p>\u67d0\u4e9b\u4e8b\u4ef6\u5728\u6d4f\u89c8\u5668\u4e2d\u9700\u8981\u505a\u8282\u6d41\u6216\u8005\u9632\u6296\uff0c\u8fd9\u65f6\u4f7f\u7528requestAnimationFrame \u66ff\u4ee3setTimeout \u53ef\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002<br \/>\n\u7c7b\u4f3c\u4e0b\u9762\u7684\u9632\u6296\u51fd\u6570\uff0c<\/p>\n<pre><code class=\"language-js\">function rafThrottle(func) {\n  let lock = false;\n  return function (...args) {\n    if (lock) return;\n    lock = true;\n    window.requestAnimationFrame(() =&gt; {\n      func.apply(this, args);\n      lock = false;\n    });\n  };\n}\n<\/code><\/pre>\n<h2>\u65f6\u95f4\u5207\u7247(Time Slicing)<\/h2>\n<p>JavaScript \u4f5c\u4e3a\u5355\u7ebf\u7a0b\u8bed\u8a00\uff0c\u5f53\u8fd0\u884c\u957f\u4efb\u52a1\u65f6\u4f1a\u963b\u585e\u5176\u4ed6\u884c\u4e3a\uff0c\u5728web \u4e0a\u6765\u770b\u5c31\u50cf\u662f\u9875\u9762\u5047\u6b7b\u3002<\/p>\n<p>\u5f53\u5ef6\u8fdf\u8d85\u8fc7100ms\uff0c\u7528\u6237\u5c31\u4f1a\u5bdf\u89c9\u5230\u5ef6\u8fdf\u611f\u3002<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u901a\u5e38\u4f1a\u91c7\u7528<a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/Web_Workers_API\/Using_web_workers\">Web Worker<\/a> \u6216\u8005\u65f6\u95f4\u5207\u7247(Time Slicing)\u3002<\/p>\n<p>Web Worker \u91c7\u7528\u4e86\u5355\u72ec\u5f00\u7ebf\u7a0b\u800c\u4e0d\u963b\u585e\u7528\u6237\u884c\u4e3a\u7684\u65b9\u5f0f\u6765\u8fdb\u884c\u4efb\u52a1\uff0c\u8fd9\u91cc\u4e0d\u518d\u7ec6\u8bf4\u3002<\/p>\n<p>\u800c\u65f6\u95f4\u5207\u7247\u662f\u91c7\u7528\u5206\u5272\u957f\u4efb\u52a1\u7684\u65b9\u5f0f\u6765\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff0c\u9650\u5236\u4efb\u52a1\u7684\u53ef\u6267\u884c\u65f6\u95f4\uff0c\u4ece\u800c\u4e0d\u5f71\u54cd\u7528\u6237\u4ea4\u4e92\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u90fd 2022 \u5e74\u4e86\uff0c\u8fd8\u4e0d\u5728\u9875\u9762\u4e2d\u52a0\u70b9\u52a8\u753b\u5417\uff1f \u5408\u7406\u9ad8\u6548\u7684\u52a8\u753b\u4f1a\u8ba9\u7528\u6237\u611f\u89c9\u7684\u4f53\u9a8c\u4e0e\u4ea4\u4e92\u66f4\u597d\uff0c\u5728 web \u4e2d\uff0c\u5e38\u5e38\u4f7f\u7528 CSS \u52a8\u753b\uff08transition \u4e0e keyFrames\uff09\u3001SVG \u52a8\u753b\uff08S\u2026<\/p>\n","protected":false},"author":2,"featured_media":262,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"_links":{"self":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/258"}],"collection":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=258"}],"version-history":[{"count":1,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/258\/revisions"}],"predecessor-version":[{"id":259,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/258\/revisions\/259"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/media\/262"}],"wp:attachment":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}