编程语言
首页 > 编程语言> > Javascript定时通知 – setTimeout,setInterval

Javascript定时通知 – setTimeout,setInterval

作者:互联网

我正在创建一个允许用户管理日历的网络应用程序(CRUD事件,任务,提醒等…)

我正在尝试实现一个功能,他们将在事件/任务前x分钟收到弹出提醒.根据我的理解,使用javascript只有一种方法可以做到这一点:

在登录时,检查数据库中的任何即将发生的事件(比如在接下来的12小时内)并为下一个事件创建一个setTimeout,当setTimeout执行时,再次检查下一个事件,等等……

我的问题是,在用户交互期间在后台运行多个setTimeouts(10)会降低我的应用程序的性能吗?

有没有更好的方法来处理客户端的弹出通知?推送通知?任何建议将不胜感激!

解决方法:

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

在那些数字中,没有. (取决于10中的情况.我的意思是,我预计一百万可能是一个问题.)

另一种方法是使用一个计时器(例如,每分钟)来检查应该在该分钟发生的通知.例如.:

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

标签:javascript,performance,settimeout,reminders
来源: https://codeday.me/bug/20190728/1564408.html