Search code examples
javascriptperformancesettimeoutreminders

Javascript Timed Notifications - setTimeout, setInterval


I am creating a web app that allows users to manage a calendar (CRUD events, tasks, reminders etc...)

And I am trying to implement a feature where they will receive a popup reminder x-minutes before the event/task. From my understanding there is really only one way to do this with javascript:

On login, check for any upcoming events in the database (say in the next 12 hours) and create a setTimeout for the next event, when that setTimeout executes, check again for next event and so on...

My question is, will having multiple setTimeouts (10+) running in the background during user interaction slow down the performance of my app?

Is there a better way to handle popup notifications on the client side? Push Notifications? Any suggestions would be greatly appreciated!


Solution

  • My question is, will having multiple setTimeouts (10+) running in the background during user interaction slow down the performance of my app?

    In those numbers, no. (Depending on how + the + in 10+ is. I mean, I expect a million probably would be an issue.)

    The other approach would be to have a single timer that you use (say, per minute) to check for notifications that should occur as of that minute. E.g.:

    function notifyForThisMinute() {
        // Notify user of things we should notify them of as of this minute
        // ...
    
        // Schedule next check for beginning of next minute; always wait
        // until we're a second into the minute to make the checks easier
        setTimeout(notifyForThisMinute, (61 - new Date().getSeconds()) * 1000);
    }
    notifyForThisMinute(); // First call starts process