简体   繁体   English

如何根据其拥有的实时时间而不是其设备中的时间来计时Node js中的功能每6小时工作一次?

[英]how can timing a function in node js to work every 6 hours Depending on the real time he has and not the time in his Device?

var time= new Date().getHours();

if(time== "6"){
       //function Do stuff
}
if(time== "12"){
       //function Do stuff
}
if(time== "18"){
       //function Do stuff
}
if(time== "24"){
       //function Do stuff
}

but when change time in my lab to any of these hours the function call 但是当我在实验室中将时间更改为以下任何时间时,函数调用

how can to get real time in his zone ? 如何获得他所在区域的实时信息?

You can use the node schedule library. 您可以使用节点计划库。 https://www.npmjs.com/package/node-schedule https://www.npmjs.com/package/node-schedule

var schedule = require('node-schedule');

schedule.scheduleJob("*/6 * * *", function() {
     //function Do stuff every 6 hours
});

Cron job format Cron作业格式

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23) 
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

Alternatively a more verbose method 另一种更详细的方法

schedule.scheduleJob({hour: 6, minute: 0, dayOfWeek: 0}, function(){
   //function Do stuff every 6 hours
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM