Javascript 定时通知 - setTimeout、setInterval

标签 javascript performance settimeout reminders

我正在创建一个允许用户管理日历(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/

相关文章:

javascript - 使用 chart.js 的实时图表

javascript - 在 val 中设置变量

scala - 为什么 clojure 在 alioth 基准测试中的表现比 scala 差

performance - 用户空间与内核空间程序性能差异

java - 如何让Rhino运行自定义的javascript setTimeout函数,无尽的eval

javascript - 如何在 AngularJS ng-click 触发器之前设置超时

javascript - 根据 style.right 重置位置

python - numba 编译逻辑比较中的性能损失

javascript - 如何使用 setTimeout 或 setInterval 在 Javascript 中编写阶乘函数

Javascript - 使用复选框输入返回动态行数据