{"id":3664,"date":"2024-11-10T15:14:03","date_gmt":"2024-11-10T07:14:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3664\/"},"modified":"2024-11-10T15:14:03","modified_gmt":"2024-11-10T07:14:03","slug":"%e5%9c%a8%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e4%b8%ad%e4%bd%bf%e7%94%a8-setinterval-%e6%97%b6%ef%bc%8c%e4%b8%ba%e4%bb%80%e4%b9%88-this-%e4%bc%9a%e6%8c%87%e5%90%91-window-%e5%af%b9%e8%b1%a1%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3664\/","title":{"rendered":"\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528 setInterval \u65f6\uff0c\u4e3a\u4ec0\u4e48 this \u4f1a\u6307\u5411 window \u5bf9\u8c61\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173111392573754.jpg\" class=\"aligncenter\" title=\"\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528 setInterval \u65f6\uff0c\u4e3a\u4ec0\u4e48 this \u4f1a\u6307\u5411 window \u5bf9\u8c61\uff1f\u63d2\u56fe\" alt=\"\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528 setInterval \u65f6\uff0c\u4e3a\u4ec0\u4e48 this \u4f1a\u6307\u5411 window \u5bf9\u8c61\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528 setinterval \u7684\u5e38\u89c1\u95ee\u9898<\/strong><\/p>\n<p>\u5f53\u6211\u4eec\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528 setinterval \u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230 this \u6307\u5411\u95ee\u9898\uff0c\u5bfc\u81f4 setinterval \u4ec5\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<p>\u8fd9\u4e3b\u8981\u662f\u7531\u4e8e javascript \u4e2d this \u7684\u7ed1\u5b9a\u673a\u5236\u3002\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0cthis \u88ab\u7ed1\u5b9a\u5230\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u5f53\u6211\u4eec\u4f7f\u7528 setinterval \u65f6\uff0c\u8ba1\u65f6\u5668\u56de\u8c03\u51fd\u6570\u662f\u5728\u5168\u5c40\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u7684\uff0c\u6b64\u65f6 this \u6307\u5411 window \u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u7684\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528 bind \u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre>this.circle = this.circle.bind(this);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>bind \u65b9\u6cd5\u5c06\u4e00\u4e2a\u51fd\u6570\u7ed1\u5b9a\u5230\u6307\u5b9a\u7684\u5bf9\u8c61\uff0c\u56e0\u6b64\u5728\u8ba1\u65f6\u5668\u56de\u8c03\u51fd\u6570\u4e2d\uff0cthis \u5c06\u6307\u5411\u7ed1\u5b9a\u7684\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff1a<\/strong><\/p>\n<pre>setInterval(() =&gt; {\n  this.circle();\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7bad\u5934\u51fd\u6570\u4f1a\u7ee7\u627f\u5176\u7236\u7ea7\u4e2d\u7684 this \u503c\uff0c\u56e0\u6b64\u5728\u8ba1\u65f6\u5668\u56de\u8c03\u51fd\u6570\u4e2d\uff0cthis \u5c06\u81ea\u52a8\u6307\u5411\u521b\u5efa\u7684\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528 setInterval \u65f6\uff0c this \u4f1a\u6307\u5411 window \u5bf9\u8c61\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528 setinterval \u7684\u5e38\u89c1\u95ee\u9898 \u5f53\u6211\u4eec\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528 setinterval \u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230 this \u6307\u5411\u95ee\u9898\uff0c\u5bfc\u81f4 setinterval \u4ec5\u6267\u884c\u4e00\u6b21\u3002 \u8fd9\u4e3b\u8981\u662f\u7531\u4e8e javascript \u4e2d this \u7684\u7ed1\u5b9a\u673a\u5236\u3002\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0cthis \u88ab\u7ed1\u5b9a\u5230\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u5f53\u6211\u4eec\u4f7f\u7528 setinterval \u65f6\uff0c\u8ba1\u65f6\u5668\u56de\u8c03\u51fd\u6570\u662f\u5728\u5168\u5c40\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u7684\uff0c\u6b64\u65f6 this \u6307\u5411 window \u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u7684\u5bf9\u8c61\u3002 \u89e3\u51b3\u65b9\u6848\uff1a \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\uff1a \u4f7f\u7528 bind \u65b9\u6cd5\uff1a this.circle = this.circle.bind(this); \u767b\u5f55\u540e\u590d\u5236 bind \u65b9\u6cd5\u5c06\u4e00\u4e2a\u51fd\u6570\u7ed1\u5b9a\u5230\u6307\u5b9a\u7684\u5bf9\u8c61\uff0c\u56e0\u6b64\u5728\u8ba1\u65f6\u5668\u56de\u8c03\u51fd\u6570\u4e2d\uff0cthis \u5c06\u6307\u5411\u7ed1\u5b9a\u7684\u5bf9\u8c61\u3002 \u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff1a setInterval(() =&gt; { this.circle(); }); \u767b\u5f55\u540e\u590d\u5236 \u7bad\u5934\u51fd\u6570\u4f1a\u7ee7\u627f\u5176\u7236\u7ea7\u4e2d\u7684 this \u503c\uff0c\u56e0\u6b64\u5728\u8ba1\u65f6\u5668\u56de\u8c03\u51fd\u6570\u4e2d\uff0cthis \u5c06\u81ea\u52a8\u6307\u5411\u521b\u5efa\u7684\u5bf9\u8c61\u3002 \u4ee5\u4e0a\u5c31\u662f\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528 setInterval \u65f6\uff0c this \u4f1a\u6307\u5411 window \u5bf9\u8c61\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3664","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3664"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3664\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}