【问题标题】:Error when try to use bind of node-schedule尝试使用节点调度绑定时出错
【发布时间】:2023-08-10 03:37:02
【问题描述】:

我正在尝试使用节点调度,并且我想在将来使用当前数据。 所以我的代码是:

var j = schedule.scheduleJob({
       start: new Date().setHours(data[i].start_hour, data[i].start_minute),
       end: new Date().setHours(data[i].end_hour, data[i].end_minute),
       rule: '*/'+distance_run+' * * * * *'
}, function (data) {
       console.log("333", data)
}).bind(null, data[i]);

但有错误:schedule.scheduleJob(...).bind is not a function

请帮帮我!

【问题讨论】:

    标签: node.js cron schedule


    【解决方案1】:

    scheduleJob() 不返回可以执行的函数。如果你想在函数内部使用data[i],你可以将data[i]绑定函数作为第二个参数传递,

    var j = schedule.scheduleJob({
           start: new Date().setHours(data[i].start_hour, data[i].start_minute),
           end: new Date().setHours(data[i].end_hour, data[i].end_minute),
           rule: '*/'+distance_run+' * * * * *'
    }, function (data) {
           console.log("333", data)
    }.bind(null, data[i]) );
    

    【讨论】:

      最近更新 更多