4

我正在创建一个应用程序,javascript在每个星期日上午 12 点发送通知。
我应该怎么做才能在那个时候调用一个函数。

4

2 回答 2

12

我不会用 javascript 来做

那说(大喊大叫……)

function foo(){
    var day =new Date().getDay();
    var hours =new Date().getHours();
    
    if (day === 0 && hours >12 && hours < 13)  // day is a 0 index base
                                               // sunday between 12:00 and 13:00
        // Do what you want here:
}

setInterval(foo, 3600000); // one hour check.

现场演示
​</p>

于 2012-05-09T13:00:42.213 回答
0

另一个解决方案是第 3 方,例如:cron

var CronJob = require('cron').CronJob;
var job = new CronJob('0 0 0 * * 0', function() {
  console.log('You will see this message every sunday, 12 am');
}, null, true, 'America/Los_Angeles');
job.start();
于 2022-02-21T08:52:29.250 回答