{"id":315491,"date":"2024-05-21T00:02:02","date_gmt":"2024-05-20T16:02:02","guid":{"rendered":""},"modified":"2024-05-21T00:02:17","modified_gmt":"2024-05-20T16:02:17","slug":"%e5%89%8d%e7%ab%af-js-%e9%a1%b9%e7%9b%ae%e5%ae%9e%e7%8e%b0%e5%80%92%e8%ae%a1%e6%97%b6%e7%9a%84%e6%96%b9%e5%bc%8f%e6%9c%89%e5%93%aa%e4%ba%9b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/315491.html","title":{"rendered":"\u524d\u7aef js \u9879\u76ee\u5b9e\u73b0\u5012\u8ba1\u65f6\u7684\u65b9\u5f0f\u6709\u54ea\u4e9b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27035229\/db91a540-f6ce-4957-b12a-571d4bcbc946.webp\" alt=\"\u524d\u7aef js \u9879\u76ee\u5b9e\u73b0\u5012\u8ba1\u65f6\u7684\u65b9\u5f0f\u6709\u54ea\u4e9b\" \/><\/p>\n<p><p>\u5b9e\u73b0\u524d\u7aefJavaScript\uff08JS\uff09\u9879\u76ee\u4e2d\u5012\u8ba1\u65f6\u529f\u80fd\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u51e0\u79cd\uff1a<strong>\u4f7f\u7528\u539f\u751fJS\u5b9e\u73b0\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3001Web Workers\u4f7f\u7528\u4ee5\u53ca\u65f6\u95f4\u670d\u52a1\u5668\u540c\u6b65<\/strong>\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>\u4f7f\u7528\u539f\u751fJS\u5b9e\u73b0\u662f\u6700\u57fa\u7840\u4e5f\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f<\/strong>\uff0c\u5b83\u4e0d\u9700\u8981\u4f9d\u8d56\u4efb\u4f55\u5916\u90e8\u5e93\uff0c\u901a\u8fc7\u7b80\u5355\u7684\u4ee3\u7801\u5c31\u53ef\u4ee5\u5b9e\u73b0\u5012\u8ba1\u65f6\u529f\u80fd\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e3b\u8981\u901a\u8fc7\u83b7\u53d6\u76ee\u6807\u65f6\u95f4\u548c\u5f53\u524d\u65f6\u95f4\u7684\u5dee\u503c\uff0c\u7136\u540e\u7528<code>setInterval()<\/code>\u51fd\u6570\u8bbe\u7f6e\u5b9a\u65f6\u5668\uff0c\u5b9a\u671f\u66f4\u65b0\u5dee\u503c\uff0c\u4ece\u800c\u5b9e\u73b0\u5012\u8ba1\u65f6\u529f\u80fd\u3002\u7531\u4e8e\u5b83\u7684\u7b80\u5355\u6027\u548c\u6613\u7528\u6027\uff0c\u5bf9\u4e8e\u5c0f\u578b\u9879\u76ee\u6216\u8005\u5b66\u4e60JS\u7684\u521d\u5b66\u8005\u6765\u8bf4\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u539f\u751fJS\u5b9e\u73b0\u5012\u8ba1\u65f6<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u539f\u751fJS\u5b9e\u73b0\u5012\u8ba1\u65f6\u65f6\uff0c\u9996\u5148\u9700\u8981\u786e\u5b9a\u5012\u8ba1\u65f6\u7684\u76ee\u6807\u65f6\u95f4\uff0c\u7136\u540e\u8ba1\u7b97\u4e0e\u5f53\u524d\u65f6\u95f4\u7684\u5dee\u503c\u3002\u8fd9\u4e2a\u5dee\u503c\u5c31\u662f\u5012\u8ba1\u65f6\u7684\u603b\u65f6\u957f\u3002\u901a\u8fc7<code>Date<\/code>\u5bf9\u8c61\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5e76\u8ba1\u7b97\u8fd9\u4e9b\u65f6\u95f4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function countdown(targetDate) {<\/p>\n<p>    const target = new Date(targetDate).getTime(); \/\/ \u76ee\u6807\u65f6\u95f4\u7684\u65f6\u95f4\u6233<\/p>\n<p>    const interval = setInterval(() =&gt; {<\/p>\n<p>        const now = new Date().getTime(); \/\/ \u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233<\/p>\n<p>        const distance = target - now; \/\/ \u65f6\u95f4\u5dee<\/p>\n<p>        const days = Math.floor(distance \/ (1000 * 60 * 60 * 24));<\/p>\n<p>        const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) \/ (1000 * 60 * 60));<\/p>\n<p>        const minutes = Math.floor((distance % (1000 * 60 * 60)) \/ (1000 * 60));<\/p>\n<p>        const seconds = Math.floor((distance % (1000 * 60)) \/ 1000);<\/p>\n<p>        \/\/ \u66f4\u65b0\u5012\u8ba1\u65f6\u663e\u793a<\/p>\n<p>        \/\/ document.getElementById(&quot;countdown&quot;).innerHTML = days + &quot;d &quot; + hours + &quot;h &quot; + minutes + &quot;m &quot; + seconds + &quot;s &quot;;<\/p>\n<p>        if (distance &lt; 0) {<\/p>\n<p>            clearInterval(interval);<\/p>\n<p>            \/\/ document.getElementById(&quot;countdown&quot;).innerHTML = &quot;EXPIRED&quot;;<\/p>\n<p>        }<\/p>\n<p>    }, 1000);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u7684\u5012\u8ba1\u65f6\u9700\u6c42\uff0c\u6216\u8005\u5e0c\u671b\u901a\u8fc7\u66f4\u52a0\u7b80\u6d01\u7684\u65b9\u5f0f\u5b9e\u73b0\u5012\u8ba1\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3002\u4e00\u4e9b\u6d41\u884c\u7684JavaScript\u5e93\u5982Moment.js\u3001Countdown.js\u7b49\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u4e30\u5bcc\u548c\u7075\u6d3b\u7684\u65f6\u95f4\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u4f7f\u7528Moment.js \u793a\u4f8b<\/p>\n<p>import moment from &#039;moment&#039;;<\/p>\n<p>function countdownWithMoment(targetDate) {<\/p>\n<p>    const target = moment(targetDate);<\/p>\n<p>    const interval = setInterval(() =&gt; {<\/p>\n<p>        const now = moment();<\/p>\n<p>        const duration = moment.duration(target.diff(now));<\/p>\n<p>        const days = duration.days();<\/p>\n<p>        const hours = duration.hours();<\/p>\n<p>        const minutes = duration.minutes();<\/p>\n<p>        const seconds = duration.seconds();<\/p>\n<p>        \/\/ \u66f4\u65b0\u5012\u8ba1\u65f6\u663e\u793a<\/p>\n<p>        \/\/ ...<\/p>\n<p>        if (duration.asSeconds() &lt; 0) {<\/p>\n<p>            clearInterval(interval);<\/p>\n<p>            \/\/ ...<\/p>\n<p>        }<\/p>\n<p>    }, 1000);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Web Workers\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u590d\u6742\u7684\u524d\u7aef\u5e94\u7528\u4e2d\uff0c\u82e5\u5012\u8ba1\u65f6\u529f\u80fd\u9700\u8981\u7cbe\u786e\u4e14\u4e0d\u5f71\u54cd\u4e3b\u7ebf\u7a0b\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Web Workers\u3002Web Workers\u5141\u8bb8JavaScript\u4ee3\u7801\u8fd0\u884c\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\uff0c\u5b9e\u73b0\u4e0e\u4e3b\u7ebf\u7a0b\u7684\u5206\u79bb\uff0c\u4ece\u800c\u8fbe\u5230\u4e0d\u963b\u585eUI\u6e32\u67d3\u7684\u76ee\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u4e3b\u7ebf\u7a0b\u4ee3\u7801<\/p>\n<p>const worker = new Worker(&#039;countdownWorker.js&#039;);<\/p>\n<p>worker.postMessage({targetDate: &#039;2023-12-31T23:59:59&#039;});<\/p>\n<p>worker.onmessage = function(e) {<\/p>\n<p>    const {days, hours, minutes, seconds} = e.data;<\/p>\n<p>    \/\/ \u66f4\u65b0UI<\/p>\n<p>};<\/p>\n<p>\/\/ countdownWorker.js\u4e2d\u7684\u5012\u8ba1\u65f6\u4ee3\u7801\u7c7b\u4f3c\u4e8e\u4f7f\u7528\u539f\u751fJS\u5b9e\u73b0\uff0c\u4f46\u8fd0\u884c\u5728Web Worker\u4e2d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u65f6\u95f4\u670d\u52a1\u5668\u540c\u6b65<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u9ad8\u7cbe\u5ea6\u7684\u5012\u8ba1\u65f6\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u591a\u7528\u6237\u95f4\u9700\u8981\u540c\u6b65\u663e\u793a\u76f8\u540c\u5012\u8ba1\u65f6\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u65f6\u95f4\u670d\u52a1\u5668\u540c\u6b65\u7684\u65b9\u5f0f\u3002\u8fd9\u9700\u8981\u540e\u7aef\u670d\u52a1\u5668\u652f\u6301\uff0c\u901a\u8fc7\u83b7\u53d6\u670d\u52a1\u5668\u65f6\u95f4\u800c\u975e\u5ba2\u6237\u7aef\u65f6\u95f4\u6765\u8ba1\u7b97\u5012\u8ba1\u65f6\uff0c\u4ee5\u907f\u514d\u5ba2\u6237\u7aef\u65f6\u95f4\u4e0d\u51c6\u786e\u5e26\u6765\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u73b0\u65f6\uff0c\u9996\u5148\u901a\u8fc7AJAX\u6216\u5176\u4ed6HTTP\u8bf7\u6c42\u83b7\u53d6\u670d\u52a1\u5668\u7aef\u7684\u5f53\u524d\u65f6\u95f4\uff0c\u7136\u540e\u6839\u636e\u8fd9\u4e2a\u65f6\u95f4\u6765\u542f\u52a8\u5012\u8ba1\u65f6\u3002\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u6709\u6548\u907f\u514d\u56e0\u5ba2\u6237\u7aef\u65f6\u95f4\u8bef\u5dee\u5bfc\u81f4\u7684\u5012\u8ba1\u65f6\u4e0d\u51c6\u786e\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u793a\u4f8b\u4f2a\u4ee3\u7801<\/p>\n<p>function syncCountdown(endpoint, targetDate) {<\/p>\n<p>    fetch(endpoint)<\/p>\n<p>        .then(response =&gt; response.json())<\/p>\n<p>        .then(serverTime =&gt; {<\/p>\n<p>            const serverNow = new Date(serverTime.now).getTime();<\/p>\n<p>            \/\/ \u4e4b\u540e\u7684\u5012\u8ba1\u65f6\u8ba1\u7b97\u4e0e\u66f4\u65b0\u903b\u8f91\u57fa\u4e8eserverNow\u8fdb\u884c<\/p>\n<p>        });<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u5408\u4ee5\u4e0a\uff0c\u5b9e\u73b0\u524d\u7aefJS\u9879\u76ee\u4e2d\u7684\u5012\u8ba1\u65f6\u529f\u80fd\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u6bcf\u79cd\u65b9\u5f0f\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u3002\u9009\u62e9\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u3001\u6027\u80fd\u8981\u6c42\u4ee5\u53ca\u5f00\u53d1\u8d44\u6e90\u7b49\u56e0\u7d20\u7efc\u5408\u8003\u8651\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u524d\u7aefJS\u9879\u76ee\u5982\u4f55\u5b9e\u73b0\u5012\u8ba1\u65f6\uff1f<\/strong><\/p>\n<p>\u5012\u8ba1\u65f6\u5e38\u7528\u4e8e\u5012\u8ba1\u65f6\u6d3b\u52a8\u3001\u79d2\u6740\u65f6\u95f4\u7b49\u573a\u666f\uff0c\u524d\u7aefJS\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\u5012\u8ba1\u65f6\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528<code>setTimeout<\/code>\u6216<code>setInterval<\/code>\u51fd\u6570\uff0c\u6bcf\u9694\u4e00\u79d2\u66f4\u65b0\u5012\u8ba1\u65f6\u7684\u663e\u793a\uff0c\u76f4\u5230\u5012\u8ba1\u65f6\u7ed3\u675f\u3002<\/li>\n<li>\u901a\u8fc7<code>Date<\/code>\u5bf9\u8c61\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u548c\u76ee\u6807\u65f6\u95f4\u7684\u5dee\u503c\uff0c\u5e76\u5c06\u5dee\u503c\u8f6c\u6362\u4e3a\u5929\u3001\u5c0f\u65f6\u3001\u5206\u949f\u3001\u79d2\u7684\u5f62\u5f0f\u8fdb\u884c\u663e\u793a\u3002<\/li>\n<li>\u4f7f\u7528<code>requestAnimationFrame<\/code>\u51fd\u6570\u5b9e\u73b0\u52a8\u753b\u6548\u679c\u7684\u5012\u8ba1\u65f6\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u662f\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408CSS\u52a8\u753b\u5236\u4f5c\u70ab\u9177\u7684\u5012\u8ba1\u65f6\u6548\u679c\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5904\u7406\u5012\u8ba1\u65f6\u7ed3\u675f\u540e\u7684\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>\u5012\u8ba1\u65f6\u7ed3\u675f\u540e\uff0c\u53ef\u4ee5\u5728JS\u4e2d\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u663e\u793a\u5012\u8ba1\u65f6\u7ed3\u675f\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/li>\n<li>\u8df3\u8f6c\u5230\u6307\u5b9a\u7684\u9875\u9762\u6216\u94fe\u63a5\u3002<\/li>\n<li>\u6267\u884c\u7279\u5b9a\u7684\u51fd\u6570\u6216\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5728\u5012\u8ba1\u65f6\u7ed3\u675f\u65f6\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\u6765\u5904\u7406\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5b9e\u73b0\u591a\u4e2a\u5012\u8ba1\u65f6\u7684\u7ba1\u7406\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u9875\u9762\u4e2d\u9700\u8981\u540c\u65f6\u7ba1\u7406\u591a\u4e2a\u5012\u8ba1\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u5bf9\u8c61\u6216\u6570\u7ec4\u6765\u5b58\u50a8\u591a\u4e2a\u5012\u8ba1\u65f6\u7684\u76f8\u5173\u6570\u636e\uff0c\u4f8b\u5982\u76ee\u6807\u65f6\u95f4\u3001\u5f53\u524d\u65f6\u95f4\u7b49\u3002<\/li>\n<li>\u901a\u8fc7\u5faa\u73af\u904d\u5386\u7684\u65b9\u5f0f\uff0c\u5bf9\u6bcf\u4e2a\u5012\u8ba1\u65f6\u8fdb\u884c\u66f4\u65b0\u548c\u663e\u793a\u3002<\/li>\n<li>\u5728\u5012\u8ba1\u65f6\u7ed3\u675f\u65f6\u5c06\u5176\u4ece\u5bf9\u8c61\u6216\u6570\u7ec4\u4e2d\u79fb\u9664\uff0c\u4ee5\u4fbf\u7ba1\u7406\u548c\u91ca\u653e\u8d44\u6e90\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f83\u597d\u5730\u5b9e\u73b0\u591a\u4e2a\u5012\u8ba1\u65f6\u7684\u7ba1\u7406\u548c\u663e\u793a\uff0c\u786e\u4fdd\u9875\u9762\u4e2d\u591a\u4e2a\u5012\u8ba1\u65f6\u7684\u51c6\u786e\u6027\u548c\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b9e\u73b0\u524d\u7aefJavaScript\uff08JS\uff09\u9879\u76ee\u4e2d\u5012\u8ba1\u65f6\u529f\u80fd\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u51e0\u79cd\uff1a\u4f7f\u7528\u539f\u751fJS\u5b9e\u73b0\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3001Web [&hellip;]","protected":false},"author":3,"featured_media":315512,"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\/315491"}],"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=315491"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/315491\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/315512"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=315491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=315491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=315491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}