K
K
knime2020-10-19 17:26:54
Node.js
knime, 2020-10-19 17:26:54

How to get time until end of setTimeout ( Node.js )?

I write a bot in VK. I need to know the time until the end of the running timer.
I tried to equate the timer to a variable, as it was in the documentation, nothing happened.
Here is an example piece of code:

cmd.hear(/^(?:час)$/i, async (message, bot) => {
  if(message.user.timer === true)
        {
        return bot(`Осталось: ${время setTimeout / 60 / 1000} минут.`);
        }
  
    else
      {
        setTimeout(() => {
            message.user.timer = false;
            console.log('Relog');
        }, 3600000);

        message.user.timer = true;
       }
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WbICHA, 2020-10-19
@WblCHA

As far as I know, there is no way to find out how much time is left before the timeout is executed. At least for the reason that he himself does not know the exact time before execution, but only knows how long he cannot be performed.
But this is if we talk about pulling information out of the timeout, and of course you can:
const timerExecuteTime = Date.now() + 3600000;

N
Nadim Zakirov, 2020-10-19
@zkrvndm

When you create a timer, just in parallel also make a note somewhere about the execution time:

timer = setTimeout(function() {
    // Ваш код
}, 5000);
start_time = new Date().getTime() + 5000;

Well, or write a wrapper function for setTimeout (), which will remember the time for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question