0

I am creating reminder bot. User inputs date e.g 01.01.19, I'm using new Date() to get milisecs to event, then I call setTimeout() function and wait until it happens. So the problem is about setTimeout() Is it OK to call it for a long period of time and many times? If user will call it for e.g. 10000 times, will it break my bot and so on? Is it optimal case or you can advice something else

  • 1
    See [this question](https://stackoverflow.com/questions/3468607/why-does-settimeout-break-for-large-millisecond-delay-values) about the limits of `setTimeout`. You plan has other issues too. What happens if you need to restart the process? Do you lose all the reminders? – Mark Dec 09 '18 at 00:36
  • I have a database, so i w'ont lose them – Jesusss Dec 09 '18 at 00:58
  • Have you considered looking at something like https://www.npmjs.com/package/node-schedule ? When the server starts, you can query the database for any reminders that need to be loaded, create the scheduled jobs, and then any new ones that get added, save them to a database, and then add those to the scheduler as well. – Joshua Terrill Dec 10 '18 at 09:41

1 Answers1

1

Well since you have a database, just do the following:

  1. When the user clicks the button, add a new reminder in the database
  2. Have an interval timer that constantly iterates through all items in the database and checks if they have passed their time yet
  3. Show those ones that need to be reminded.

Should be pretty simple to do - just use setInterval, a loop, and a little bit of working with Date in JavaScript.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • _that constantly iterates through all items in the database_, hmm for 10K or more records!? Maybe calculate the date when reminder should be run, store reminder with that date and select only actual reminders. – alexmac Dec 09 '18 at 02:01
  • @alexmac But how will you find out when reminders should be run if there are 10K or more of them, all with different dates? How will you go through them all and say *"That date has passed - that reminder must be alerted! That one hasn't - ignore it."*? – Jack Bashford Dec 09 '18 at 02:05
  • user already provides that date: _User inputs date e.g 01.01.19_. The query should select all reminders where `reminder.runDate <= now`. – alexmac Dec 09 '18 at 02:09
  • Ah, I get that - I'm not good with DB or queries, so I didn't get that you don't have to loop through them to select them. Thanks! – Jack Bashford Dec 09 '18 at 02:10