在一天中的特定时间做某事js

Do something at a specific time of the day js

本文关键字:js 定时间 一天      更新时间:2023-09-26

如何在特定的时间和日期激发函数例如:每周三8:30。我能想到的解决方案是用一个间隔检查时间,但循环很滞后,尤其是当你必须每分钟检查一次时。有其他解决方案吗。请帮忙,谢谢。

类似。。。

var dateItHappens = new Date()
// ... set the dateItHappens variable up ...
var millisTillOccurence = dateItHappens.getTime() - new Date().getTime();
setTimeout(function(){ /* ... function you want to call ... */ }, millisTillOccurence);

如果使用node,则可以使用node-cron包,和cron.schedule('***,async()=>{//你的任务)

第一颗星代表秒,下一分钟等等……