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){

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


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.