E
E
enjoyka2021-11-12 17:08:38
Node.js
enjoyka, 2021-11-12 17:08:38

Why does setInterval in the bot body fire from 2 to 8 times instead of 1 time?

There is a simple bot, it has a function output in its body via setInterval (here replaced with console.log) every second (in the original I check records in the database and send), in fact, every second the bot performs the same action from 2 up to 8 times randomly, although in theory it should execute 1 time in a second, what could be the problem? I give a simplified code example, even the console log it spits out text in a second at a time, so it's not about the function:

try {

Bot.start(async ctx => {

// Here is any bot activity like Bot.hears

(() => setInterval (async () => await console.log("Just text"), 1000 * 60))();
} catch (error) {
console.error(error);
}

And now in the console every second "Just text" crashes 8 times, sometimes 2 or 6 times, it's not clear how ....

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Pershin, 2021-11-12
@Get-Web

Tie with setInterval and switch to setTimeout, and call a new function after the previous one has completed, otherwise the more the server is loaded, the more your functions that setInterval calls will hang in the queue. As a result, they are not executed every second, but as soon as the queue reaches them, since the time for their execution has expired long ago and it must be executed as soon as the opportunity arises. Well, that's one of the possible reasons.

And now in the console every second "Just text" crashes 8 times, sometimes 2 or 6 times, it's not clear how ....

Just very similar to the situation that I described. Given that requests, asynchronous responses can come at different speeds, as a result, their execution is delayed, and more and more new requests fly up and accumulate by executing callback functions in batches

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question