{"id":344569,"date":"2021-02-13T20:35:13","date_gmt":"2021-02-13T12:35:13","guid":{"rendered":"http:\/\/4563.org\/?p=344569"},"modified":"2021-02-13T20:35:13","modified_gmt":"2021-02-13T12:35:13","slug":"js-%e6%80%8e%e4%b9%88%e5%9c%a8%e9%a1%b5%e9%9d%a2%e5%ae%9e%e7%8e%b0%e5%bb%b6%e6%97%b6%e6%8f%90%e9%86%92%ef%bc%9f%ef%bc%88%e5%a4%96%e9%83%a8-api-%e8%af%b7%e6%b1%82%e6%85%a2-23-%e7%a7%92%e8%bf%94","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=344569","title":{"rendered":"JS \u600e\u4e48\u5728\u9875\u9762\u5b9e\u73b0\u5ef6\u65f6\u63d0\u9192\uff1f\uff08\u5916\u90e8 API \u8bf7\u6c42\u6162 2~3 \u79d2\u8fd4\u56de\u6570\u636e | \u4e5f\u53ef\u80fd\u5931\u8fde\uff1a\u8ba9\u7528\u6237\u91cd\u65b0\u52a0\u8f7d\u4e00\u4e0b\uff09"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  JS \u600e\u4e48\u5728\u9875\u9762\u5b9e\u73b0\u5ef6\u65f6\u63d0\u9192\uff1f\uff08\u5916\u90e8 API \u8bf7\u6c42\u6162 2~3 \u79d2\u8fd4\u56de\u6570\u636e | \u4e5f\u53ef\u80fd\u5931\u8fde\uff1a\u8ba9\u7528\u6237\u91cd\u65b0\u52a0\u8f7d\u4e00\u4e0b\uff09               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : liudaqi <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u9875\u9762\u4e0a\u6709\u4e00\u4e2a\u5916\u90e8\u5730\u56fe\u6570\u636e\u7edf\u8ba1\u7684 API \u8bf7\u6c42\uff0c\u901a\u5e38\u4f1a\u6709 2~3 \u79d2\u7684\u5ef6\u65f6\uff0c\u6570\u636e\u597d\u4e86\u518d\u5f02\u6b65\u52a0\u8f7d\u663e\u793a\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u7531\u4e8e\u8fd9\u4e2a API \u6bd4\u8f83\u62e5\u6324\uff0c\u53ef\u80fd\u4f1a\u8bf7\u6c42\u62d2\u7edd\uff0c\u7136\u540e\u5728\u9875\u9762\u4e0a\u63d0\u793a\u7528\u6237\uff1a\u5982\u679c\u8981\u770b\u5730\u56fe\u5c55\u793a\u7684\u7edf\u8ba1\u6570\u636e\uff0c\u53ef\u4ee5\u91cd\u65b0\u5237\u65b0\u4e00\u4e0b\uff08\u4e0d\u770b\u5730\u56fe\u6570\u636e\u5c55\u793a\uff0c\u5e76\u4e0d\u5f71\u54cd\u9875\u9762\u5176\u4ed6\u90e8\u5206\uff09\u3002<\/p>\n<p>\u73b0\u5728\u7ecf\u5e38\u4f1a\u51fa\u73b0\u9519\u8bef\u63d0\u793a\uff1a\u8ba9\u7528\u6237\u91cd\u65b0\u5237\u65b0\u4e00\u4e0b\uff0c\u5176\u5b9e\u662f\u8fc7 2~3 \u79d2\u4e4b\u540e\u6570\u636e\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u600e\u4e48\u8ba9\u8fd9\u4e2a\u63d0\u793a\u665a 5 \u79d2\u949f\u5de6\u53f3\u518d\u51fa\u73b0\uff1f\uff08\u8d85\u8fc7 5 \u79d2\u6570\u636e\u8fd8\u6ca1\u51c6\u5907\u597d\uff0c\u5927\u6982\u7387\u8fd9\u6b21\u7f51\u7edc\u8bf7\u6c42\u662f\u62ff\u4e0d\u5230\u6570\u636e\u4e86\uff0c\u4e0d\u662f\u901f\u5ea6\u6162\u7684\u60c5\u51b5\u3002\u8fd9\u4e2a\u65f6\u5019\u53bb\u63d0\u9192\u7528\u6237\uff09      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5313511\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ferock <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             setTimeOut                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313512\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : liudaqi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ferock setTimeOut \u4e0d\u884c\uff0c\u53ea\u662f\u628a\u9519\u8bef\u7684\u63d0\u793a\u5ef6\u65f6\u51fa\u73b0\u4e86\uff0c\u8fd8\u662f\u4f1a\u51fa\u73b0\u3002<\/p>\n<p>\u671f\u671b\u7684\u60c5\u51b5\u662f\uff1a5 \u79d2\u5185\u7f51\u7edc\u6570\u636e\u52a0\u8f7d\u5b8c\u4e86\uff0c\u5c31\u4e0d\u63d0\u793a\uff1b\u6ca1\u52a0\u8f7d\u597d\u5c31\u63d0\u793a\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313513\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Zhuzhuchenyan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u5728\u52a0\u8f7d\u5b8c\u4e86 clear timeout https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/WindowTimers\/clearTimeout                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313514\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Rocketer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Promise.race \u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6267\u884c\u5b8c\u6210\u7684\u7ed3\u679c\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u7528\u5b83\u540c\u65f6\u6267\u884c\u4e00\u4e2a 5 \u79d2\u949f\u5ef6\u65f6\u7684 reject \u3002\u5982\u679c\u8bf7\u6c42\u6210\u529f\u4e86\u5c31\u4f1a\u5ffd\u7565\u8fd9\u4e2a reject\uff0c\u5982\u679c 5 \u79d2\u8fd8\u6ca1\u6210\u529f\uff0c\u5c31\u4f1a\u6536\u5230\u8fd9\u4e2a reject \u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<p>&#8220;`Javascript<br \/>const result = await Promise.race([<br \/> fetch(&#8216;\/your-request-api&#8217;),<br \/> new Promise((resolve, reject) =&gt; {<br \/> setTimeout(() =&gt; reject(\u2018Timeout\u2019), 5000)<br \/> })<br \/>]);<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313515\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : a570295535 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @liudaqi \u524d\u53f0\u5f04\u4e00\u4e2a 3 \u4e2a\u70b9\u7684 gif \u52a8\u56fe\u663e\u793a\u52a0\u8f7d\u4e2d\uff0cajax \u8bf7\u6c42\u5730\u56fe api \u5224\u65ad\u662f\u5426\u8fd4\u56de 200\uff0c\u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e 5 \u79d2\uff0c\u8d85\u65f6\u5c31\u8fd4\u56de\u5237\u65b0\u63d0\u793a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313516\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SilencerL <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0a Promise.race \u6b63\u89e3, \u8bdd\u8bf4\u8fd9\u4e0d\u662f\u524d\u7aef\u57fa\u7840\u77e5\u8bc6\u4e86\u5417..                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313517\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : learningman <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8bf7\u6c42\u7684\u65f6\u5019\u8bbe timeout\uff0c\u7136\u540e\u628a\u663e\u793a\u63d0\u793a\u7684\u4ee3\u7801\u653e catch \u91cc\u4e0d\u5c31\u597d\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313518\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : molvqingtai <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8ba9\u7528\u6237\u4e3b\u52a8\u5237\u65b0\u4f53\u9a8c\u4e0d\u597d\uff0c\u8bf7\u6c42\u5931\u8d25\u4e86\u5c31\u81ea\u52a8\u91cd\u8bd5\uff0c\u4e2d\u9014\u4e00\u76f4\u663e\u793a loading                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313519\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xstmjh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             new Promise(function (resolve, reject) {<br \/> asyncFn(param, function (err, result) {<br \/> if (error) {<br \/> return reject(error);<br \/> }<br \/> return resolve(result);<br \/> });<\/p>\n<p> setTimeout(function () {<br \/> reject(&#8216;timeout&#8217;);<br \/> }, 5000);<br \/>}).then(doSomething);                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313520\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ljpCN <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5efa\u8bae\u4e3b\u8be6\u7ec6\u8bf4\u8bf4\u8fd9\u4e2a\u9519\u8bef\u63d0\u793a\u662f\u5728\u4ec0\u4e48\u60c5\u5f62\u4e0b\u629b\u51fa\u7684\u3002\u770b\u4f60\u7684\u63cf\u8ff0\uff0c\u5e76\u4e0d\u662f\u5728\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25\u65f6\u629b\u51fa\u7684\uff0c\u5982\u679c\u662f\u5e94\u8be5\u5c31\u4e0d\u5b58\u5728\u9700\u8981\u6dfb\u52a0\u5ef6\u65f6\u7684\u95ee\u9898\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313521\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Reapper <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b9a\u65f6\u5668                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313522\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Roger006 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1 \u53d1\u9001\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u540c\u65f6\u8bbe\u7f6e settimeout [\u8bb0\u5f97\u4fdd\u5b58\u53e5\u67c4\uff0c\u6e05\u9664\u5ef6\u65f6\u65f6\u8981\u7528\u5230] \uff0ctimer<br \/>2 \u5ef6\u65f6\u8981\u8fdb\u884c\u7684\u64cd\u4f5c\u5c31\u662f\u63d0\u793a\u7528\u6237\u5237\u65b0\u7684\u5f39\u7a97\u65b9\u6cd5\uff0c\u6b64\u5904\u5047\u8bbe\u65b9\u6cd5\u540d\u4e3a waringFn<br \/>3 \u5199\u4e00\u4e2a\u6e05\u7406\u5ef6\u65f6\u7684\u65b9\u6cd5\uff0c\u6bd4\u5982 clearTimerFn = ()=&gt;{clearTimeout(timer); timer = null;}<br \/>4 \u8bf7\u6c42\u672c\u8eab\u8bbe\u5b9a 5 \u79d2\u8d85\u65f6\uff0ccatch \u91cc\u8fb9 [\u6b64\u5904\u6355\u83b7\u5f02\u5e38\u5305\u542b\u8d85\u65f6\u3001\u7f51\u7edc\u95ee\u9898\u3001\u7aef\u53e3\u62d2\u7edd\u7b49] \u8c03\u7528 waringFn \u548c cleaerTimerFn \uff08 catch \u91cc\u8fb9\u7684\u64cd\u4f5c\u9700\u8981\u4e00\u4e2a\u5224\u65ad\uff0c\u5224\u65ad timer \u662f\u5426\u4e3a null \uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313523\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : John60676 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u662f\u7528 vue3 \u5199\u7684\u8bdd\uff0c\u53ef\u4ee5\u8003\u8651\u4e00\u4e0b\u7528 vue-request https:\/\/github.com\/AttoJS\/vue-request \u6765\u5904\u7406\uff0c\u91cc\u9762\u6709\u4e2a \u9519\u8bef\u91cd\u8bd5 \u7684\u6a21\u5757\uff0c\u5e94\u8be5\u80fd\u89e3\u51b3\u4f60\u9762\u4e34\u7684\u95ee\u9898\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>JS \u600e\u4e48\u5728\u9875\u9762\u5b9e\u73b0\u5ef6\u65f6\u63d0\u9192\uff1f\uff08\u5916&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/344569"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=344569"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/344569\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=344569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=344569"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=344569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}