Javascript 如何在电报节点bot中存储超时对象来启动和停止计时器?
所以,我想在电报机器人中实现定时器。A知道如何启动它,但我找不到通过命令阻止它的方法。这是一个密码Javascript 如何在电报节点bot中存储超时对象来启动和停止计时器?,javascript,node.js,timer,telegram,Javascript,Node.js,Timer,Telegram,所以,我想在电报机器人中实现定时器。A知道如何启动它,但我找不到通过命令阻止它的方法。这是一个密码 class TimerController extends TelegramBaseController { startTimer($) { let timer = setInterval(function () { $.sendMessage('Minute passed') }, 60000) $.setChatSession('timer', time
class TimerController extends TelegramBaseController {
startTimer($) {
let timer = setInterval(function () {
$.sendMessage('Minute passed')
}, 60000)
$.setChatSession('timer', timer)
.then(() => {
console.log('stored')
})
.catch(reason => {
console.log(reason)
})
}
stopTimer($) {
$.getChatSession('timer')
.then(timer => {
clearInterval(timer)
})
.catch(reason => {
console.log(reason)
})
}
get routes() {
return {
'timerCommand': 'startTimer',
'stopTimerCommand': 'stopTimer',
}
}
}
它无法工作,因为无法字符串化超时对象。因此,我无法在处理程序之间存储有关活动计时器的信息。我自己发现了这种解决方法,但不确定它是否足够好。现在计时器监视会话并等待命令停止
class TimerController extends TelegramBaseController {
startTimer($) {
setInterval(function () {
$.getChatSession('timerStop')
.then((timerStop) => {
if (timerStop === true) {
console.log('stoping timer')
clearInterval(this)
} else {
$.sendMessage('Minute passed')
}
})
.catch(reason => {
console.log(reason)
})
}, 60000)
$.setChatSession('timerStop', false)
.then(() => {
console.log('timer started')
})
.catch(reason => {
console.log(reason)
})
}
stopTimer($) {
$.setChatSession('timerStop', true)
.then( () => {
console.log('ordering to stop timer')
})
.catch(reason => {
console.log(reason)
})
}
get routes() {
return {
'timerCommand': 'startTimer',
'stopTimerCommand': 'stopTimer',
}
}
}
我尝试使用外部变量,但有8个并行电报工作者,并且不能保证stopTimer和startTimer处理程序将在同一进程中。