{"id":1663,"date":"2020-03-05T12:17:12","date_gmt":"2020-03-05T04:17:12","guid":{"rendered":"http:\/\/ykyi.net\/?p=1663"},"modified":"2020-03-05T12:17:12","modified_gmt":"2020-03-05T04:17:12","slug":"%e8%ae%b2%e8%ae%b2%e4%b8%ba%e4%bb%80%e4%b9%88%e8%a6%81%e7%94%a8javascript%e7%9a%84requestanimationframe%e5%87%bd%e6%95%b0%e5%9c%a8web%e4%b8%8a%e5%ae%9e%e7%8e%b0%e5%a4%8d%e6%9d%82%e5%8a%a8%e7%94%bb","status":"publish","type":"post","link":"https:\/\/ykyi.net\/?p=1663","title":{"rendered":"\u8bb2\u8bb2\u4e3a\u4ec0\u4e48\u8981\u7528javascript\u7684requestAnimationFrame\u51fd\u6570\u5728web\u4e0a\u5b9e\u73b0\u590d\u6742\u52a8\u753b\u6548\u679c"},"content":{"rendered":"<p>\u7b80\u5355\u5730\u8bb2\uff0crequestAnimationFrame\u51fd\u6570\u66f4\u9002\u5408\u5728\u6d4f\u89c8\u5668\u4e0a\u9762\u505a\u590d\u6742\u7684\u52a8\u753b\u3002\u6bd4\u7528\u5b9a\u65f6\u5668\u6548\u7387\u9ad8\u3002<\/p>\n<p>\u5e9f\u8bdd\u5982\u4e0b\uff1a<\/p>\n<p>\u73b0\u4ee3\u7684WEB\u4e16\u754c(2020\u5e74)\uff0c\u7f51\u9875\u4e0a\u5404\u79cd\u5404\u6837\u7684\u5185\u5bb9\u4e30\u5bcc\u591a\u5f69\u3002\u867d\u7136css\u4e5f\u80fd\u5b8c\u6210\u5f88\u591a\u7b80\u5355\u7684\u52a8\u753b\uff0c\u4f46\u662f\u4ecd\u7136\u6709\u4e00\u4e9b\u590d\u6742\u7684\u52a8\u753banimation\u9700\u6c42\u9700\u8981javascript\u5904\u7406\u3002\u4e4b\u524d\uff0c\u6211\u7b80\u5355\u5730\u7406\u89e3\uff0c\u6d4f\u89c8\u5668\u5df2\u7ecf\u6709\u5b9a\u65f6\u5668\u51fd\u6570setTimeout()\u548csetInterval(),\u5c31\u8db3\u591f\u505a\u51fa\u590d\u6742\u7684\u52a8\u753b\u5f62\u5f0f\u3002\u539f\u6765\uff0c\u4e0d\u662f\u8fd9\u6837\u7684\u3002<\/p>\n<p>\u8fd9\u6bb5\u65f6\u95f4\u5728\u505a\u4e00\u4e2a\u8fd0\u884c\u5728\u6d4f\u89c8\u5668\u4e0a\u7684\u7ea2\u767d\u673a\u6a21\u62df\u5668\uff0c\u53c2\u8003\u5f00\u6e90\u4ee3\u7801\uff0c\u53d1\u73b0\u7528\u7684\u662frequestAnimationFrame()\u51fd\u6570\u3002\u4e8e\u662f\uff0c\u67e5\u4e86\u4e00\u4e9b\u8d44\u6599\uff0c\u6da8\u4e86\u77e5\u8bc6\u3002<\/p>\n<h4>\u9996\u5148\uff0c\u4f7f\u7528\u5b9a\u65f6\u5668SetTimeout\u548csetInterval\u505a\u52a8\u753b\u6709\u4e24\u4e2a\u5927\u7f3a\u70b9\u3002<\/h4>\n<ol>\n<li>\u53d7\u6709\u9650\u7684\u8d44\u6e90\uff0c\u6216\u8005\u7cdf\u7cd5\u7684cpu\u5bc6\u96c6\u7684\u4e1a\u52a1\u4ee3\u7801\u7684\u5f71\u54cd\u3002\u6d4f\u89c8\u5668\u5e76\u4e0d\u4e25\u683c\u6309\u7167\u5b9a\u65f6\u5668\u51fd\u6570\u4e2d\u7684\u65f6\u95f4\u95f4\u9694\u53c2\u6570\u6765\u8c03\u5ea6\u5b9a\u65f6\u56de\u8c03\u51fd\u6570\u3002\u6bd4\u598250ms\uff0c\u6709\u65f6\u4f1a\u5feb\uff0c\u6709\u65f6\u4f1a\u6162\u3002\u8fd9\u6837\uff0c\u52a8\u753b\u6548\u679c\u7684\u5e27\u4e0e\u5e27\u4e4b\u95f4\u7684\u753b\u9762\u5c31\u4e0d\u4f1a\u90a3\u4e48\u6d41\u7545\u4e86\u3002<\/li>\n<li>\u9891\u7e41\u7684\u8c03\u7528\u8ba1\u65f6\u5668\u51fd\u6570setTimeout\u6216setInterval\uff0c\u53ef\u80fd\u5bfc\u81f4\u6d4f\u89c8\u5668\u5047\u6b7b\u3002\u6d4f\u89c8\u5668\u7aed\u5c3d\u5168\u529b\u7684\u5904\u7406\u4e00\u4e9b\u8ddf\u52a8\u753b\u65e0\u5173\u7684\u4ee3\u7801\uff0c\u6bd4\u5982\u6587\u6863\u6d41\u7684\u4e0d\u65ad\u91cd\u6392\u3002\u8fd9\u6837\uff0c\u7528\u6237\u7684\u754c\u9762\u6839\u672c\u6ca1\u6709\u83b7\u5f97\u91cd\u65b0\u6e32\u67d3\u7684\u673a\u4f1a\u3002\u7279\u522b\u5728\u624b\u673a\u6d4f\u89c8\u5668\uff0c\u7531\u4e8e\u624b\u673a\u7684\u73af\u5883\u6bd4\u8f83\u7279\u6b8a\u3002\u624b\u673a\u7684\u7535\u6c60\u53ef\u4ee5\u4e0d\u591f\uff0c\u7f51\u7edc\u4e0d\u597d\uff0c\u90fd\u53ef\u80fd\u5f71\u54cdweb\u9875\u9762\u7684\u6587\u6863\u91cd\u6392\uff08page reflow\uff09\u7684\u6548\u7387\u3002<\/li>\n<\/ol>\n<h3>\u4e3a\u4ec0\u4e48\u7528requestAnimationFrame<\/h3>\n<ol>\n<li>\u9488\u5bf9\u5b9a\u65f6\u5668\u7684\u7f3a\u70b91\uff0c\u6d4f\u89c8\u5668\u7684\u5b9e\u73b0\u4fdd\u8bc1requestAnimationFrame\u8bbe\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u4f1a\u4e25\u683c\u5b9a\u65f6\u6267\u884c\u3002\u5f53\u7136\u662f\u5728web\u9875\u662f\u6d3b\u8dc3\u72b6\u6001\uff0c\u5373\u4e0d\u662f\u5728\u540e\u53f0\u8fd0\u884c\u3002\u7b49\u4e0b\u4f1a\u8bb2\u5230\u540e\u53f0\u6267\u884c\u65f6\u7684\u60c5\u51b5\u3002\u4e00\u822c\u6d4f\u89c8\u5668\u8c03\u7528requestAnimationFrame\u8bbe\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u7684\u9891\u7387\u662f\u6bcf\u79d260\u6b21\u3002\u4e0d\u540c\u6d4f\u89c8\u5668\u7684\u5b9e\u73b0\u6709\u4e0d\u53d8\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53web\u9875\u9762\u88ab\u653e\u5165\u540e\u53f0\u8fd0\u884c\u65f6\uff0c\u6d4f\u89c8\u5668\u4f1a\u5927\u8f90\u964d\u4f4erequestAnimationFrame\u8bbe\u7f6e\u7684\u56de\u8c03\u51fd\u6570\u8c03\u7528\u9891\u7387\u3002\u4f4e\u81f3\u6bcf\u79d2\u4e24\u6b21\uff0c\u751a\u81f3\u6682\u505c\u3002\u8fd9\u6837\uff0c\u53c8\u80fd\u8282\u7701\u76f8\u5f53\u591a\u7684\u8d44\u6e90\u3002\u6bd4\u5982\u624b\u673a\u4e0a\u73cd\u8d35\u7684\u7535\u6c60\u8d44\u6e90\u3002\u975e\u5e38\u5bb9\u6613\u7406\u89e3\uff0cweb\u9875\u9762\u4e0d\u53ef\u89c1\u7684\u65f6\u5019\uff0c\u8fd8\u7ee7\u7eed\u6e32\u67d3\u753b\u9762\u662f\u5b8c\u5168\u6ca1\u6709\u610f\u4e49\u7684\u3002<\/p>\n<\/li>\n<\/ol>\n<h3>\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9<\/h3>\n<p>\u65e2\u7136requestAnimationFrame\u7684\u8c03\u7528\u9891\u7387\u56e0\u6d4f\u89c8\u5668\u800c\u5f02\uff0c\u53e6\u5916\u9875\u9762\u5728\u540e\u53f0\u65f6\u53c8\u53ef\u80fd\u4f1a\u6682\u505c\u3002\u6240\u4ee5\uff0c\u751f\u6210\u52a8\u753b\u7684\u4ee3\u7801\u4e0d\u80fd\u9884\u8bbe\u56de\u8c03\u51fd\u6570\u4ee5\u67d0\u4e00\u4e2a\u56fa\u5b9a\u7684\u9891\u7387\u6267\u884c\u3002\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0crequestAnimationFrame\u7684\u56de\u8c03\u51fd\u6570\u4f1a\u4f20\u5165\u4e00\u4e2a\u65f6\u95f4\u6233\u53c2\u6570\uff0c<\/p>\n<p>\u4e0b\u9762\u662f\u5b98\u65b9\u6587\u6863\u5bf9\u4f20\u5165\u56de\u8c03\u51fd\u6570\u7684\u65f6\u95f4\u53c2\u6570\u7684\u89e3\u91ca\uff0c\u7ed9\u4e86\u4e00\u4e2a\u9ad8\u7cbe\u5ea6\u7684\u65f6\u95f4\u6233\uff0c\u5355\u4f4d\u662f\u6beb\u79d2\u3002\u55ef\uff0c\u5bf9\u4e8e\u6d4f\u89c8\u5668\u6765\u8bf4\uff0c\u5982\u679c\u80fd\u7cbe\u786e\u5230\u6beb\u79d2\u7684\u8bdd\uff0c\u7b97\u5f97\u4e0a\u9ad8\u7cbe\u5ea6\u7684\u65f6\u95f4\u6233\u4e86\u3002<\/p>\n<blockquote><p>\n  The callback function is passed one single argument, a DOMHighResTimeStamp similar to the one returned by performance.now(), indicating the point in time when requestAnimationFrame() starts to execute callback functions\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u5355\u5730\u8bb2\uff0crequestAnimationFrame\u51fd\u6570\u66f4\u9002\u5408\u5728\u6d4f\u89c8\u5668\u4e0a\u9762\u505a\u590d\u6742\u7684\u52a8\u753b\u3002\u6bd4\u7528\u5b9a\u65f6\u5668\u6548\u7387\u9ad8\u3002 \u5e9f\u8bdd\u5982\u4e0b\uff1a \u73b0\u4ee3\u7684WEB\u4e16\u754c(2020\u5e74)\uff0c\u7f51\u9875\u4e0a\u5404\u79cd\u5404\u6837\u7684\u5185\u5bb9\u4e30\u5bcc\u591a\u5f69\u3002\u867d\u7136css\u4e5f\u80fd\u5b8c\u6210\u5f88\u591a\u7b80\u5355\u7684\u52a8\u753b\uff0c\u4f46\u662f\u4ecd\u7136\u6709\u4e00\u4e9b\u590d\u6742\u7684\u52a8\u753banimation\u9700\u6c42\u9700\u8981javascript\u5904\u7406\u3002\u4e4b\u524d\uff0c\u6211\u7b80\u5355\u5730\u7406\u89e3\uff0c\u6d4f\u89c8\u5668\u5df2\u7ecf\u6709\u5b9a\u65f6\u5668\u51fd\u6570setTimeout()\u548csetInterval(),\u5c31\u8db3\u591f\u505a\u51fa\u590d\u6742\u7684\u52a8\u753b\u5f62\u5f0f\u3002\u539f\u6765\uff0c\u4e0d\u662f\u8fd9\u6837\u7684\u3002 \u8fd9\u6bb5\u65f6\u95f4\u5728\u505a\u4e00\u4e2a\u8fd0\u884c\u5728\u6d4f\u89c8\u5668\u4e0a\u7684\u7ea2\u767d\u673a\u6a21\u62df\u5668\uff0c\u53c2\u8003\u5f00\u6e90\u4ee3\u7801\uff0c\u53d1\u73b0\u7528\u7684\u662frequestAnimationFrame()\u51fd\u6570\u3002\u4e8e\u662f\uff0c\u67e5\u4e86\u4e00\u4e9b\u8d44\u6599\uff0c\u6da8\u4e86\u77e5\u8bc6\u3002 \u9996\u5148\uff0c\u4f7f\u7528\u5b9a\u65f6\u5668SetTimeout\u548csetInterval\u505a\u52a8\u753b\u6709\u4e24\u4e2a\u5927\u7f3a\u70b9\u3002 \u53d7\u6709\u9650\u7684\u8d44\u6e90\uff0c\u6216\u8005\u7cdf\u7cd5\u7684cpu\u5bc6\u96c6\u7684\u4e1a\u52a1\u4ee3\u7801\u7684\u5f71\u54cd\u3002\u6d4f\u89c8\u5668\u5e76\u4e0d\u4e25\u683c\u6309\u7167\u5b9a\u65f6\u5668\u51fd\u6570\u4e2d\u7684\u65f6\u95f4\u95f4\u9694\u53c2\u6570\u6765\u8c03\u5ea6\u5b9a\u65f6\u56de\u8c03\u51fd\u6570\u3002\u6bd4\u598250ms\uff0c\u6709\u65f6\u4f1a\u5feb\uff0c\u6709\u65f6\u4f1a\u6162\u3002\u8fd9\u6837\uff0c\u52a8\u753b\u6548\u679c\u7684\u5e27\u4e0e\u5e27\u4e4b\u95f4\u7684\u753b\u9762\u5c31\u4e0d\u4f1a\u90a3\u4e48\u6d41\u7545\u4e86\u3002 \u9891\u7e41\u7684\u8c03\u7528\u8ba1\u65f6\u5668\u51fd\u6570setTimeout\u6216setInterval\uff0c\u53ef\u80fd\u5bfc\u81f4\u6d4f\u89c8\u5668\u5047\u6b7b\u3002\u6d4f\u89c8\u5668\u7aed\u5c3d\u5168\u529b\u7684\u5904\u7406\u4e00\u4e9b\u8ddf\u52a8\u753b\u65e0\u5173\u7684\u4ee3\u7801\uff0c\u6bd4\u5982\u6587\u6863\u6d41\u7684\u4e0d\u65ad\u91cd\u6392\u3002\u8fd9\u6837\uff0c\u7528\u6237\u7684\u754c\u9762\u6839\u672c\u6ca1\u6709\u83b7\u5f97\u91cd\u65b0\u6e32\u67d3\u7684\u673a\u4f1a\u3002\u7279\u522b\u5728\u624b\u673a\u6d4f\u89c8\u5668\uff0c\u7531\u4e8e\u624b\u673a\u7684\u73af\u5883\u6bd4\u8f83\u7279\u6b8a\u3002\u624b\u673a\u7684\u7535\u6c60\u53ef\u4ee5\u4e0d\u591f\uff0c\u7f51\u7edc\u4e0d\u597d\uff0c\u90fd\u53ef\u80fd\u5f71\u54cdweb\u9875\u9762\u7684\u6587\u6863\u91cd\u6392\uff08page reflow\uff09\u7684\u6548\u7387\u3002 \u4e3a\u4ec0\u4e48\u7528requestAnimationFrame \u9488\u5bf9\u5b9a\u65f6\u5668\u7684\u7f3a\u70b91\uff0c\u6d4f\u89c8\u5668\u7684\u5b9e\u73b0\u4fdd\u8bc1requestAnimationFrame\u8bbe\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u4f1a\u4e25\u683c\u5b9a\u65f6\u6267\u884c\u3002\u5f53\u7136\u662f\u5728web\u9875\u662f\u6d3b\u8dc3\u72b6\u6001\uff0c\u5373\u4e0d\u662f\u5728\u540e\u53f0\u8fd0\u884c\u3002\u7b49\u4e0b\u4f1a\u8bb2\u5230\u540e\u53f0\u6267\u884c\u65f6\u7684\u60c5\u51b5\u3002\u4e00\u822c\u6d4f\u89c8\u5668\u8c03\u7528requestAnimationFrame\u8bbe\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u7684\u9891\u7387\u662f\u6bcf\u79d260\u6b21\u3002\u4e0d\u540c\u6d4f\u89c8\u5668\u7684\u5b9e\u73b0\u6709\u4e0d\u53d8\u3002 \u5f53web\u9875\u9762\u88ab\u653e\u5165\u540e\u53f0\u8fd0\u884c\u65f6\uff0c\u6d4f\u89c8\u5668\u4f1a\u5927\u8f90\u964d\u4f4erequestAnimationFrame\u8bbe\u7f6e\u7684\u56de\u8c03\u51fd\u6570\u8c03\u7528\u9891\u7387\u3002\u4f4e\u81f3\u6bcf\u79d2\u4e24\u6b21\uff0c\u751a\u81f3\u6682\u505c\u3002\u8fd9\u6837\uff0c\u53c8\u80fd\u8282\u7701\u76f8\u5f53\u591a\u7684\u8d44\u6e90\u3002\u6bd4\u5982\u624b\u673a\u4e0a\u73cd\u8d35\u7684\u7535\u6c60\u8d44\u6e90\u3002\u975e\u5e38\u5bb9\u6613\u7406\u89e3\uff0cweb\u9875\u9762\u4e0d\u53ef\u89c1\u7684\u65f6\u5019\uff0c\u8fd8\u7ee7\u7eed\u6e32\u67d3\u753b\u9762\u662f\u5b8c\u5168\u6ca1\u6709\u610f\u4e49\u7684\u3002 \u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9 \u65e2\u7136requestAnimationFrame\u7684\u8c03\u7528\u9891\u7387\u56e0\u6d4f\u89c8\u5668\u800c\u5f02\uff0c\u53e6\u5916\u9875\u9762\u5728\u540e\u53f0\u65f6\u53c8\u53ef\u80fd\u4f1a\u6682\u505c\u3002\u6240\u4ee5\uff0c\u751f\u6210\u52a8\u753b\u7684\u4ee3\u7801\u4e0d\u80fd\u9884\u8bbe\u56de\u8c03\u51fd\u6570\u4ee5\u67d0\u4e00\u4e2a\u56fa\u5b9a\u7684\u9891\u7387\u6267\u884c\u3002\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0crequestAnimationFrame\u7684\u56de\u8c03\u51fd\u6570\u4f1a\u4f20\u5165\u4e00\u4e2a\u65f6\u95f4\u6233\u53c2\u6570\uff0c \u4e0b\u9762\u662f\u5b98\u65b9\u6587\u6863\u5bf9\u4f20\u5165\u56de\u8c03\u51fd\u6570\u7684\u65f6\u95f4\u53c2\u6570\u7684\u89e3\u91ca\uff0c\u7ed9\u4e86\u4e00\u4e2a\u9ad8\u7cbe\u5ea6\u7684\u65f6\u95f4\u6233\uff0c\u5355\u4f4d\u662f\u6beb\u79d2\u3002\u55ef\uff0c\u5bf9\u4e8e\u6d4f\u89c8\u5668\u6765\u8bf4\uff0c\u5982\u679c\u80fd\u7cbe\u786e\u5230\u6beb\u79d2\u7684\u8bdd\uff0c\u7b97\u5f97\u4e0a\u9ad8\u7cbe\u5ea6\u7684\u65f6\u95f4\u6233\u4e86\u3002 The callback function is passed one single argument, a DOMHighResTimeStamp similar to the one returned by performance.now(), indicating the point in time when requestAnimationFrame() starts to execute callback functions<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[43],"class_list":["post-1663","post","type-post","status-publish","format-standard","hentry","category-tech_articles","tag-javascript"],"_links":{"self":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1663"}],"version-history":[{"count":0,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/1663\/revisions"}],"wp:attachment":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}