Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
How can I wait for a specific system time before firing ?
I want to fire an event when seconds = 0, i.e. every minute
while (1==1) { var date = new Date(); var sec = date.getSeconds(); if(sec===0) { Do Something() } }
你不应该那样做,因为这样while你就会有一个阻塞操作。此外,在任何 JavaScript 平台上都可以做一些更好的事情,比如使用setInterval/setTimeout函数。 他们的节点文档在这里。
while
setInterval
setTimeout
如何实现您想要的快速示例:
setInterval(function() { var date = new Date(); if ( date.getSeconds() === 0 ) { DoSomething(); } }, 1000);
为了对 Node 中的计划进程进行更细粒度的控制,也许您应该检查node-cron。