Javascript 定时通知 - setTimeout, setInterval [英] Javascript Timed Notifications - setTimeout, setInterval

查看:71
本文介绍了Javascript 定时通知 - setTimeout, setInterval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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:

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

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...

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

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!

推荐答案

我的问题是,在用户交互期间在后台运行多个 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+ 中的 + 是如何.我的意思是,我预计一百万可能是一个问题.)

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

这篇关于Javascript 定时通知 - setTimeout, setInterval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆