Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
Asyncronous countdown timer

Using Node.js 16.9.0

Hi All!

So in my discord server I have a few friends who like to "Sleep Call", but need to be disconnected a few hours after they fall asleep by a moderator. I would like to add a command to my discord bot that functions like ".sleepcall 3h", and then in 3 hours they would be disconnected from the VC they are in. My current thought is to create a entry in my sql db with the disconnect time (current time + desired wait time), and then have a loop run every minute to see if anyone needs to be disconnected. Someone mentioned to me that there is a way to do that with loops instead in JS, and I need a bit of direction to get started. I think I'll be able to do all the support code, but I am a bit lost on how to make the timer. I am proficient in c++, but have never dabbled into asynchronous languages before.

How would I make a function that, after called, would wait 1 hour (for example) and then call another function WHILE also being asynchronous that allows for multiple timers to be going at once?

My sudocode:

//userID = 8 digit integer
//timeout = time until disconnect in seconds

function disconnect (userID, timeout){
   while (timeout >0){
     timeout-1 second
     wait 1 second
   }
   if (timeout == 0){
     disconnect(userID)
   }
}

I would need multiple instances of the countdown function to be able to run at the same time to allow for multiple users to use the bot

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I would recommend you to use setTimeout() and clearTimeout() functions in javascript. Read more about them here.

You would as callback function provide your disconnect function. On call javascript will start the timer and after provided amount of wait time callback function would be executed. Countdown will be happening behind the scenes (asynchronous) while every other functionality will work normally.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda