Nodejs在一次运行代码

我想在特定的时间运行我的stream程,但只是一次。 我应该使用cron作业,执行然后停止作业或使用setTimeout? 哪个更好?

更新:我在node-cron模块中find它。 我认为这比使用setTimeout更好。

date的另一个例子

var CronJob = require('cron').CronJob; var job = new CronJob(new Date(), function(){ //runs once at the specified date. }, function () { // This function is executed when the job stops }, true /* Start the job right now */, timeZone /* Time zone of this job. */ 

);

检查节点时间表包。 根据文档,你可以在一个确切的date安排一个function。

这是2012年12月21日上午5:30安排活动的文档示例 …

 var schedule = require('node-schedule'); var date = new Date(2012, 11, 21, 5, 30, 0); var j = schedule.scheduleJob(date, function(){ console.log('The world is going to end today.'); }); 

如果需要的话,你也可以取消一个工作

 j.cancel();