1

So I have nodejs running an express server and one of our endpoints needs to start a 10 minute timer, and execute some code at the end of those 10 minutes. I'm using setTimeout to do this, and storing it in a variable.

Now, if a user performs an action and makes a different api call, we no longer need to execute the code at the end of those ten minutes. But the right setTimeout to cancel depends on a special variable.

How do I keep track of many setTimeout variables? Should I have a global hashmap that maps the special variable to the setTimeout, and then in the second api call pass along the special var to indentify the right setTimeout to call clearTimeout on?

This is the only way I can think to do it, but for a Node.js server with possible heavy load, I'm not sure just adding to a hashmap or an array is the correct solution here. Thanks in advance!

// start countdown timer for 10 minutes
  let offerTimer = setTimeout(function() {  
    // some code
  }, 60000 * 0.25);

Some other express controller function

if (req.body.accepted == true) {
   // need to clear the interval associated with req.body.jobpost_id
}
Ed Urban
  • 11
  • 1
  • 1

1 Answers1

0

You can store your timers variables in global map, but this is a bad practice to set many timers in node. Instead it will be better to use Redis, end set Redis ttl per user. https://redis.io/commands/TTL. Or use other jobs schedulers (agenda, node-schedule, etc... ).