{"id":52789,"date":"2024-12-03T17:50:26","date_gmt":"2024-12-03T09:50:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52789\/"},"modified":"2024-12-03T17:50:26","modified_gmt":"2024-12-03T09:50:26","slug":"javascript-%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e4%b8%ad-setinterval-%e7%9a%84-this-%e6%8c%87%e5%90%91%e9%97%ae%e9%a2%98%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52789\/","title":{"rendered":"JavaScript \u6784\u9020\u51fd\u6570\u4e2d setInterval \u7684 this \u6307\u5411\u95ee\u9898\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>JavaScript \u6784\u9020\u51fd\u6570\u4e2d setInterval \u7684 this \u6307\u5411\u95ee\u9898\u5982\u4f55\u89e3\u51b3\uff1f<\/h1>\n<p>\u76ee\u524d\u7c73\u4e91\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJavaScript \u6784\u9020\u51fd\u6570\u4e2d setInterval \u7684 this \u6307\u5411\u95ee\u9898\u5982\u4f55\u89e3\u51b3\uff1f\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241123\/1732346498674182829b7ea.jpg\" class=\"aligncenter\" title=\"JavaScript \u6784\u9020\u51fd\u6570\u4e2d setInterval \u7684 this \u6307\u5411\u95ee\u9898\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"JavaScript \u6784\u9020\u51fd\u6570\u4e2d setInterval \u7684 this \u6307\u5411\u95ee\u9898\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728\u6784\u9020\u51fd\u6570\u91cc\u4f7f\u7528 setinterval \u65f6 this \u6307\u5411\u7684\u95ee\u9898\u89e3\u7b54<\/strong><\/p>\n<p>\u5728 javascript \u4e2d\uff0c\u6784\u9020\u51fd\u6570\u5185\u90e8\u7684 this \u6307\u5411\u5b9e\u4f8b\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u5728 setinterval \u56de\u8c03\u51fd\u6570\u4e2d\uff0cthis \u5374\u6307\u5411 window \u5bf9\u8c61\u3002\u8fd9\u4f1a\u5bfc\u81f4 this \u6307\u5411\u95ee\u9898\uff0c\u5bfc\u81f4\u65e0\u6cd5\u8bbf\u95ee\u5b9e\u4f8b\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u6b64\u95ee\u9898\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528 bind \u65b9\u6cd5\uff1a<\/strong><\/li>\n<\/ul>\n<pre>\/\/ \u521b\u5efa\u6784\u9020\u51fd\u6570\nfunction myconstructor() {\n  this.circle = function() {...};\n}\n\n\/\/ \u4f7f\u7528 bind \u7ed1\u5b9a this\nconst _this = this;\nsetinterval(function() {\n  _this.circle();\n}, 1000);<\/pre>\n<ul>\n<li><strong>\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff1a<\/strong><\/li>\n<\/ul>\n<pre>\/\/ \u521b\u5efa\u6784\u9020\u51fd\u6570\nfunction MyConstructor() {\n  this.circle = function() {...};\n}\n\n\/\/ \u4f7f\u7528\u7bad\u5934\u51fd\u6570\u4fdd\u6301 this \u6307\u5411\nsetInterval(() =&gt; {\n  this.circle();\n}, 1000);<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aJavaScript \u6784\u9020\u51fd\u6570\u4e2d setInterval \u7684 this \u6307\u5411\u95ee\u9898\u5982\u4f55\u89e3\u51b3\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u83b7\u53d6 PySide6 \u6e90\u4ee3\u7801\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u6784\u9020\u51fd\u6570\u4e2d setInterval \u7684 this \u6307\u5411\u95ee\u9898\u5982\u4f55\u89e3\u51b3\uff1f \u76ee\u524d\u7c73\u4e91\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587\u300aJavaScript \u6784\u9020\u51fd\u6570\u4e2d setInterval \u7684 this \u6307\u5411\u95ee\u9898\u5982\u4f55\u89e3\u51b3\uff1f\u300b\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60\u6587\u7ae0\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~ \u5728\u6784\u9020\u51fd\u6570\u91cc\u4f7f\u7528 setinterval \u65f6 this \u6307\u5411\u7684\u95ee\u9898\u89e3\u7b54 \u5728 javascript \u4e2d\uff0c\u6784\u9020\u51fd\u6570\u5185\u90e8\u7684 this \u6307\u5411\u5b9e\u4f8b\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u5728 setinterval \u56de\u8c03\u51fd\u6570\u4e2d\uff0cthis \u5374\u6307\u5411 window \u5bf9\u8c61\u3002\u8fd9\u4f1a\u5bfc\u81f4 this \u6307\u5411\u95ee\u9898\uff0c\u5bfc\u81f4\u65e0\u6cd5\u8bbf\u95ee\u5b9e\u4f8b\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002 \u89e3\u51b3\u65b9\u6cd5\uff1a \u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u6b64\u95ee\u9898\uff1a \u4f7f\u7528 bind \u65b9\u6cd5\uff1a \/\/ \u521b\u5efa\u6784\u9020\u51fd\u6570 function myconstructor() { this.circle = function() {&#8230;}; } \/\/ \u4f7f\u7528 bind \u7ed1\u5b9a this const _this = this; setinterval(function() { [&hellip;]<\/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-52789","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52789","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=52789"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52789\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}