如何仅触发一次计划作业触发?

时间:2019-06-14 18:26:14

标签: node.js timer schedule

我的目标是仅调度计划功能/作业一次,在当前状态下,该功能确实触发了很多次(取决于我发送的单词数) 在聊天室中输入任何单词时,将触发功能。

我尝试了这两个代码,每个都有不同的结果。

class TimerController extends Telegram.TelegramBaseController {
    timerHandler(scope) {
        if (!j && (scope.message.chat._type == 'supergroup')) {
            scope.api.sendMessage(scope.message._chat._id, '*TEST*', { 'parse_mode' : 'Markdown'});
            j = schedule.scheduleJob('*/5 * * * * *', function(){ //5 Seconds
                scope.api.sendMessage(scope.message._chat._id, '*TEST*', { 'parse_mode' : 'Markdown'});
                process.nextTick(function() {
                    console.log(scope);
                });
            });
        }
    }

这段代码应该已经完成​​了,但是奇怪的是没有。

第二个代码:

class TimerController extends Telegram.TelegramBaseController {
    timerHandler(scope) {
        if (!j && (scope.message.chat._type == 'supergroup')) {
            j = schedule.scheduleJob('*/5 * * * * *', function(){ //5 Seconds
                scope.api.sendMessage(scope.message._chat._id, '*TEST*', { 'parse_mode' : 'Markdown'});
                process.nextTick(function() {
                    console.log(scope);
                    if (j && (scope.message.chat._type == 'supergroup')) {
                        j = schedule.scheduleJob('*/5 * * * * *', function(){ //5 Seconds
                            process.cancel(function() {
                        });
                    });
                }
            });
        });
    }
}

当我使用此功能时,作业将仅触发一次,然后停止... 而且我找不到任何只能拒绝打开同一工作,而不是停止正在运行的工作的选项。

0 个答案:

没有答案