{"id":167988,"date":"2024-05-08T16:14:22","date_gmt":"2024-05-08T08:14:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/167988.html"},"modified":"2024-05-08T16:14:29","modified_gmt":"2024-05-08T08:14:29","slug":"javascript-%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-callback-%e5%9b%9e%e8%b0%83%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/167988.html","title":{"rendered":"JavaScript \u4e2d\u5982\u4f55\u4f7f\u7528 callback \u56de\u8c03\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27034055\/0177bbb7-7d70-4fa2-a619-6ebf950d4ba0.webp\" alt=\"JavaScript \u4e2d\u5982\u4f55\u4f7f\u7528 callback \u56de\u8c03\u51fd\u6570\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u4f7f\u7528callback\u56de\u8c03\u51fd\u6570\u662f\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u3001\u4e8b\u4ef6\u76d1\u542c\u3001\u4ee5\u53ca\u67d0\u4e9b\u7279\u5b9a\u529f\u80fd\u5b8c\u6210\u540e\u7684\u901a\u77e5\u65b9\u5f0f\u3002\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u70b9\uff1a<strong>\u5b9a\u4e49\u56de\u8c03\u51fd\u6570\u3001\u5c06\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u3001\u4e8b\u4ef6\u76d1\u542c\u56de\u8c03\u3001\u4ee5\u53ca\u5f02\u6b65\u64cd\u4f5c\u4e2d\u7684\u56de\u8c03<\/strong>\u3002\u5bf9\u4e8e<strong>\u5b9a\u4e49\u56de\u8c03\u51fd\u6570<\/strong>\u800c\u8a00\uff0c\u5b83\u662f\u5728\u67d0\u9879\u4efb\u52a1\u6216\u51fd\u6570\u5b8c\u6210\u540e\u9700\u8981\u6267\u884c\u7684\u90a3\u90e8\u5206\u4ee3\u7801\uff0c\u5b83\u4e0d\u4f1a\u7acb\u5373\u6267\u884c\uff0c\u800c\u662f\u5728\u5b83\u88ab\u5f15\u7528\u7684\u51fd\u6570\u4e2d\u7684\u7279\u5b9a\u64cd\u4f5c\u5b8c\u6210\u540e\u624d\u6267\u884c\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728JavaScript\u4e2d\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5f02\u6b65\u7f16\u7a0b\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1\uff0c\u800c\u4e0d\u5fc5\u7b49\u5f85\u67d0\u4e2a\u64cd\u4f5c\u7684\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e00\u3001\u5b9a\u4e49\u56de\u8c03\u51fd\u6570<\/strong><\/h3>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u9996\u5148\u9700\u8981\u4e86\u89e3\u5b83\u662f\u4e00\u4e2a\u88ab\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u5230\u53e6\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u5e76\u5728\u8be5\u51fd\u6570\u4f53\u5185\u90e8\u88ab\u6267\u884c\u7684\u51fd\u6570\u3002\u521b\u5efa\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u5e76\u4e0d\u590d\u6742\uff0c\u5173\u952e\u5728\u4e8e\u5982\u4f55\u5c06\u8fd9\u4e2a\u51fd\u6570\u4e0e\u5176\u4ed6\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56de\u8c03\u51fd\u6570\u6848\u4f8b\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5904\u7406\u6d88\u606f\u7684\u51fd\u6570\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u5904\u7406\u5b8c\u6d88\u606f\u540e\uff0c\u6267\u884c\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u6765\u663e\u793a\u6d88\u606f\u5904\u7406\u5df2\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function showMessage(message, callback) {<\/p>\n<p>    console.log(message);<\/p>\n<p>    callback();<\/p>\n<p>}<\/p>\n<p>function showCompleted() {<\/p>\n<p>   console.log(&quot;\u6d88\u606f\u5904\u7406\u5b8c\u6210\u3002&quot;);<\/p>\n<p>}<\/p>\n<p>showMessage(&quot;\u5904\u7406\u4e2d\u7684\u6d88\u606f...&quot;, showCompleted);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>showMessage<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u3002\u5b83\u9996\u5148\u6253\u5370\u6d88\u606f\uff0c\u7136\u540e\u6267\u884c\u56de\u8c03\u51fd\u6570<code>showCompleted<\/code>\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e8c\u3001\u5c06\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012<\/strong><\/h3>\n<\/p>\n<p><p>\u5728JavaScript\u5f02\u6b65\u7f16\u7a0b\u4e2d\uff0c\u5c06\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002\u8fd9\u5141\u8bb8\u7a0b\u5e8f\u5728\u6267\u884c\u4e00\u7cfb\u5217\u64cd\u4f5c\u540e\uff0c\u6267\u884c\u56de\u8c03\u51fd\u6570\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function downloadFile(url, callback) {<\/p>\n<p>    console.log(`\u5f00\u59cb\u4e0b\u8f7d\u6587\u4ef6: ${url}`);<\/p>\n<p>    \/\/ \u6a21\u62df\u6587\u4ef6\u4e0b\u8f7d<\/p>\n<p>    setTimeout(() =&gt; {<\/p>\n<p>        \/\/ \u5047\u8bbe\u6587\u4ef6\u540d\u662f\u4eceURL\u4e2d\u63d0\u53d6\u7684\u6700\u540e\u4e00\u90e8\u5206<\/p>\n<p>        const fileName = url.split(&#039;\/&#039;).pop();<\/p>\n<p>        callback(fileName);<\/p>\n<p>    }, 2000); \/\/ \u6a21\u62df\u4e0b\u8f7d\u9700\u89812\u79d2\u949f<\/p>\n<p>}<\/p>\n<p>downloadFile(&#039;http:\/\/example.com\/file.zip&#039;, function(name) {<\/p>\n<p>   console.log(`\u4e0b\u8f7d\u5b8c\u6210, \u6587\u4ef6\u540d: ${name}`);<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u4e00\u4e2a\u7b80\u5355\u7684\u56de\u8c03\u51fd\u6570\u4f20\u9012\u7ed9\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\uff08\u5982\u6587\u4ef6\u4e0b\u8f7d\uff09\u7684\u51fd\u6570\u3002\u8fd9\u4f7f\u5f97\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u53d8\u5f97\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e09\u3001\u4e8b\u4ef6\u76d1\u542c\u4e2d\u7684\u56de\u8c03<\/strong><\/h3>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u4e8b\u4ef6\u76d1\u542c\u5e7f\u6cdb\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\u7528\u6237\u7684\u4ea4\u4e92\u6216\u5176\u4ed6\u4e8b\u4ef6\u6e90\u7684\u901a\u77e5\u3002\u901a\u8fc7\u5411\u4e8b\u4ef6\u76d1\u542c\u5668\u6ce8\u518c\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5f53\u4e8b\u4ef6\u89e6\u53d1\u65f6\uff0c\u8fd9\u4e2a\u51fd\u6570\u5c06\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.getElementById(&#039;myButton&#039;).addEventListener(&#039;click&#039;, function() {<\/p>\n<p>    console.log(&quot;\u6309\u94ae\u88ab\u70b9\u51fb\u4e86\u3002&quot;);<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u4e00\u4e2a\u533f\u540d\u56de\u8c03\u51fd\u6570\u88ab\u7528\u4f5c\u70b9\u51fb\u4e8b\u4ef6\u7684\u5904\u7406\u5668\u3002\u8be5\u533f\u540d\u51fd\u6570\u76f4\u63a5\u5b9a\u4e49\u5728\u4e8b\u4ef6\u76d1\u542c\u5668\u4e2d\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u6377\u65b9\u5f0f\u6765\u5904\u7406\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><h3><strong>\u56db\u3001\u5f02\u6b65\u64cd\u4f5c\u4e2d\u7684\u56de\u8c03<\/strong><\/h3>\n<\/p>\n<p><p>JavaScript\u7684\u5f02\u6b65\u7279\u6027\u4f7f\u5f97\u5728\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u6210\u4e3a\u53ef\u80fd\u3002\u5229\u7528\u56de\u8c03\u51fd\u6570\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u6b65\u64cd\u4f5c\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u4f7f\u7528setTimeout\u6765\u6a21\u62df\u5f02\u6b65\u64cd\u4f5c<\/p>\n<p>function fakeAsyncOperation(callback) {<\/p>\n<p>    console.log(&quot;\u5f00\u59cb\u5f02\u6b65\u64cd\u4f5c...&quot;);<\/p>\n<p>    setTimeout(() =&gt; {<\/p>\n<p>        \/\/ \u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210<\/p>\n<p>        callback(&quot;\u64cd\u4f5c\u5b8c\u6210&quot;);<\/p>\n<p>    }, 1000);<\/p>\n<p>}<\/p>\n<p>fakeAsyncOperation(result =&gt; {<\/p>\n<p>    console.log(result); \/\/ \u663e\u793a\u201c\u64cd\u4f5c\u5b8c\u6210\u201d<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u6f14\u793a\u4e86\u5982\u4f55\u5728\u5f02\u6b65\u64cd\u4f5c\uff08\u8fd9\u91cc\u662f\u4e00\u4e2a\u6a21\u62df\u7684\u5ef6\u65f6\u64cd\u4f5c\uff09\u5b8c\u6210\u540e\uff0c\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\u64cd\u4f5c\u7684\u7ed3\u679c\u3002\u8fd9\u79cd\u6a21\u5f0f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u7279\u522b\u662f\u5728\u5904\u7406HTTP\u8bf7\u6c42\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><h3><strong>\u5c0f\u7ed3<\/strong><\/h3>\n<\/p>\n<p><p>\u56de\u8c03\u51fd\u6570\u662fJavaScript\u5f02\u6b65\u7f16\u7a0b\u7684\u6838\u5fc3\u673a\u5236\u4e4b\u4e00\u3002\u901a\u8fc7\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u3001\u5728\u4e8b\u4ef6\u76d1\u542c\u4e2d\u4f7f\u7528\u3001\u4ee5\u53ca\u5728\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u56de\u8c03\uff0cJavaScript\u5f00\u53d1\u8005\u80fd\u591f\u7f16\u5199\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u4ee3\u7801\u3002\u6b63\u786e\u5730\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u5c3d\u7ba1\u8fc7\u5ea6\u4f7f\u7528\u6216\u4e0d\u5f53\u4f7f\u7528\u4e5f\u53ef\u80fd\u5bfc\u81f4\u201c\u56de\u8c03\u5730\u72f1\u201d\uff08callback hell\uff09\u7684\u95ee\u9898\uff0c\u4f46\u901a\u8fc7\u5408\u7406\u7684\u8bbe\u8ba1\u548c\u73b0\u4ee3JavaScript\u7684\u5f02\u6b65\u89e3\u51b3\u65b9\u6848\uff08\u5982Promises\u548casync\/aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t\uff09\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u8fd9\u4e00\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4e3a\u4ec0\u4e48\u5728JavaScript\u4e2d\u4f7f\u7528\u56de\u8c03\u51fd\u6570callback\uff1f<\/strong><br \/>\n\u56de\u8c03\u51fd\u6570\u5728JavaScript\u4e2d\u662f\u4e00\u79cd\u5e38\u7528\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u3002\u7531\u4e8eJavaScript\u662f\u5355\u7ebf\u7a0b\u7684\uff0c\u65e0\u6cd5\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u5f02\u6b65\u64cd\u4f5c\uff08\u5982\u7f51\u7edc\u8bf7\u6c42\uff09\u5f80\u5f80\u9700\u8981\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u624d\u80fd\u8fd4\u56de\u7ed3\u679c\u3002\u901a\u8fc7\u4f7f\u7528\u56de\u8c03\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u540e\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\uff0c\u4ee5\u4fbf\u5904\u7406\u8fd4\u56de\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528\u56de\u8c03\u51fd\u6570callback\uff1f<\/strong><br \/>\n\u5728JavaScript\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u533f\u540d\u51fd\u6570\u6216\u547d\u540d\u51fd\u6570\u6765\u5b9a\u4e49\u56de\u8c03\u51fd\u6570\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5c06\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u9700\u8981\u6267\u884c\u5f02\u6b65\u64cd\u4f5c\u7684\u51fd\u6570\u3002\u5f53\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u5904\u7406\u8fd4\u56de\u7684\u7ed3\u679c\u6216\u6267\u884c\u7279\u5b9a\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u56de\u8c03\u51fd\u6570\u4f20\u9012\u7ed9setTimeout()\u51fd\u6570\uff0c\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u540e\u6267\u884c\u8be5\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p><strong>3. \u6709\u6ca1\u6709\u5176\u4ed6\u66ff\u4ee3\u56de\u8c03\u51fd\u6570\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\uff1f<\/strong><br \/>\n\u9664\u4e86\u4f7f\u7528\u56de\u8c03\u51fd\u6570\uff0c\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u3002\u4f8b\u5982\uff0cES6\u5f15\u5165\u4e86Promise\u3001Async\/Await\u7b49\u529f\u80fd\uff0c\u4f7f\u5f02\u6b65\u4ee3\u7801\u66f4\u6613\u4e8e\u9605\u8bfb\u548c\u7ef4\u62a4\u3002\u4f7f\u7528Promise\u53ef\u4ee5\u5c06\u5f02\u6b65\u64cd\u4f5c\u5c01\u88c5\u6210\u53ef\u94fe\u63a5\u7684\u94fe\u5f0f\u8c03\u7528\uff0c\u800cAsync\/Await\u7ed3\u5408\u4f7f\u7528Promise\u53ef\u4ee5\u4ee5\u540c\u6b65\u7684\u65b9\u5f0f\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u662f\u4e3a\u4e86\u89e3\u51b3\u56de\u8c03\u5730\u72f1\uff08callback hell\uff09\u800c\u8bbe\u8ba1\u7684\uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u66f4\u52a0\u4f18\u96c5\u5730\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\uff0c\u4f7f\u7528callback\u56de\u8c03\u51fd\u6570\u662f\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u3001\u4e8b\u4ef6\u76d1\u542c\u3001\u4ee5\u53ca\u67d0\u4e9b\u7279\u5b9a\u529f\u80fd\u5b8c\u6210\u540e\u7684\u901a\u77e5\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":167999,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/167988"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=167988"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/167988\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/167999"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=167988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=167988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=167988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}