{"id":11659,"date":"2023-02-24T09:34:58","date_gmt":"2023-02-24T01:34:58","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=11659"},"modified":"2023-02-24T09:34:58","modified_gmt":"2023-02-24T01:34:58","slug":"jquery-each-%e5%85%a7%e7%9a%84-sleep-%e8%a9%b2%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%be","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=11659","title":{"rendered":"jQuery - $.each \u5167\u7684 sleep() \u8a72\u5982\u4f55\u5be6\u73fe"},"content":{"rendered":"<p>--<\/p>\n<h2>\u554f\u984c<\/h2>\n<pre class=\"lang:js decode:true\">let $q = [];\r\n$.each( $q, function(k,v){\r\n    const xhr = new XMLHttpRequest();\r\n    xhr.responseType = 'blob';\r\n    xhr.open(\"POST\", '\/download_word\/' + v, true);\r\n\r\n    xhr.onload = function (e) {\r\n        let blob = e.currentTarget.response;\r\n        if ( blob.size &gt; 0 ) saveBlob(blob, 'word_'+ v + '.doc');\r\n    };\r\n    xhr.send();\r\n} );<\/pre>\n<p><span style=\"color: #333333; font-style: normal; font-weight: 400;\">\u539f\u4f86\u6709\u4e00\u500b\u9663\u5217\uff0c\u4f7f\u7528\u88e1\u9762\u7684\u8cc7\u6599\u975e\u540c\u6b65\u53bb\u53d6\u5f97\u8cc7\u6599\u4e26\u4e0b\u8f09\uff0c\u56e0\u70ba\u975e\u540c\u6b65\u5e7e\u4e4e\u662f\u540c\u6642\uff0c\u6240\u4ee5\u700f\u89bd\u5668\u9700\u8981\u958b\u555f\u540c\u6642\u4e0b\u8f09\u591a\u6a94\u6848\u6b0a\u9650\uff0c\u53ef\u662f\u5728\u6578\u91cf\u591a\u6642\u6703\u5c11\u6a94\u6848\uff0cF12 \u76e3\u63a7\u7db2\u8def\u662f\u5168\u90e8\u7684\u6a94\u6848\u90fd\u6709\u8a2a\u554f\uff0c\u53ef\u662f\u4e0b\u8f09\u7684\u6578\u91cf\u4e0d\u5c0d\uff0c\u9019\u6642\u5c31\u8981\u628a\u4e0b\u8f09\u88fd\u9020\u9593\u9694\u8b8a\u6210\u55ae\u4e00\u3001\u55ae\u4e00\u7684\u6a94\u6848\u4e0b\u8f09<\/span><\/p>\n<p>\u7db2\u8def\u4e0a\u6703\u6284\u5230\u985e\u4f3c\u9019\u6a23\u7684\u4ee3\u78bc (<a href=\"https:\/\/stackoverflow.com\/questions\/951021\/what-is-the-javascript-version-of-sleep\" target=\"_blank\" rel=\"noopener\">What is the JavaScript version of sleep()?<\/a>)<\/p>\n<pre class=\"lang:js decode:true \">function sleep(ms) {\r\n    return new Promise(resolve =&gt; setTimeout(resolve, ms));\r\n}<\/pre>\n<p>\u53ef\u60dc\u5c0d Hoyo \u7684\u61c9\u7528\u5834\u666f\u6c92\u7528\uff0c\u6240\u4ee5\u53ea\u80fd\u81ea\u5df1\u60f3\u8fa6\u6cd5\uff0c\u5f8c\u4f86\u60f3\u5230\u4e5f\u53ea\u80fd\u7528 setInterval()<\/p>\n<p>--<\/p>\n<h2>\u89e3\u6cd5<\/h2>\n<pre class=\"lang:js decode:true \">\/\/ \u8ca0\u8cac\u4e0b\u8f09\r\nfunction run(filename, url, v){\r\n    if ( v !== undefined ) {\r\n        const xhr = new XMLHttpRequest();\r\n        xhr.responseType = 'blob';\r\n        xhr.open(\"POST\", url + v, true);\r\n\r\n        xhr.onload = function (e) {\r\n            let blob = e.currentTarget.response;\r\n            if (blob.size &gt; 0) saveBlob(blob, filename + v + '.doc');\r\n        };\r\n        xhr.send();\r\n    }\r\n}\r\n\r\n\/\/ \u8655\u7406\u8cc7\u6599 \u5206\u5272\u6642\u5e8f\r\nfunction download_word(){\r\n    let $q = explode(',', $('#id_form').find('[name=\"subject\"]').val());\r\n\r\n    let i = 0;\r\n    let $int = setInterval(function(){\r\n        if ( $q[i]===undefined ) clearInterval($int);\r\n        run('word_', '\/download_word\/', $q[i]);\r\n        i++;\r\n    }, 250);\r\n}<\/pre>\n<ul>\n<li>\u539f\u4f86\u7684\u8cc7\u6599\u9663\u5217\u4f7f\u7528 setInterval() \u8f2a\u8a62\uff0c\u6c92\u8cc7\u6599\u6642\u5c31 clearInterval() \u522a\u9664<\/li>\n<li>\u7136\u5f8c\u4e0b\u8f09\u5c31\u7368\u7acb\u51fa\u4f86<\/li>\n<\/ul>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"11659\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,387&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- \u554f\u984c let $q = ...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"11659\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;2,387&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[262],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/11659"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11659"}],"version-history":[{"count":2,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/11659\/revisions"}],"predecessor-version":[{"id":11763,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/11659\/revisions\/11763"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}