我正在创建一个允许用户管理日历(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 定时通知 - setTimeout、setInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20670273/