Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda