How do I make timeouts for different users in Node JS?

  • 0
    I am making a telegram bot on Node, the essence of which is to create timers for users. The user can stop the timer at any time. How do I get him to clear his timer?

    My decision as a novice developer. Created an array, into which I add the {user_id, timeout} object when creating a timer. And when the user clicks on the stop, I make a clearTimeout for the desired object and then delete the object itself from the array.

    function clearUserTimeout(user_id) {
        const indexOfTimeout = timeouts.findIndex(item => item.user_id === user_id)
        if (indexOfTimeout !== -1) {
            timeouts.splice(indexOfTimeout, 1)

    How to do it right?
    JavaScript Lydia Lynn, May 18, 2019

  • 1 Answers
  • 0
    I would store in the object not timer objects, but only the time in UNIX, at the moment the user should stop the timer and make 1 common timer, which every second (1.5-2) runs through the objects and checks the time specified there, checks with the current and if it is less than the current one, it sends a message to the user and ends its timer by changing some bool variable, or simply by specifying the time to null.

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!