ClearInterval don't work or work but I make a mistake. I don't know but when I use /stop it continue write 'Sending'. How to resolve such problem.
bot.hears(/\/send|\/stop/, ctx=> { let sending = setInterval(() => { if (/\/send/.test(ctx.update.message.text)) { ctx.reply('Sending:'); } else if (/\/stop/.test(ctx.update.message.text)){ ctx.reply('stopping;'); clearInterval(sending), } }; 10000); });
The main problem is you're creating new intervals every time you send /send
or /stop
. So, your intervals get created multiple times generating multiple intervals in parallel.
Something like this should work:
let sendInterval;
bot.hears(/\/send|\/stop/, ctx => {
if (sendInterval) {
clearInterval(sendInterval);
}
if (/\/send/.test(ctx.update.message.text)) {
sendInterval = setInterval(() => {
ctx.reply('Sending');
}, 10000);
} else if (/\/stop/.test(ctx.update.message.text)) {
ctx.reply('stopping!');
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.