angular - setTimeout 在 for 循环内发送通知
问题描述
我正在尝试根据某些参数发送通知,并且尝试使用 for 循环和 setTimeout,但是当我运行它时,所有通知都会立即发送。代码如下所示:
this.times 是一个 n 维数组。this.timer 是基于用户输入的变量
for(let i of this.times) {
this.localNotification()
}
localNotification() {
setTimeout(() => {
this.date = new Date()
this.localNotifications.schedule({
text: "Hey it's time to take a picture",
trigger: {at: new Date(new Date().getTime())},
led: 'FF0000',
sound: 'file:/storage/emulated/0/media/audio/notifications/CwtChime.ogg'
})
this.notificationList.unshift({Title: "Time to take a picture", Body: "Hey, it's been a week since you took a picture, please take one", Reminder: true, Time: `${this.date.toLocaleString()}`, Seen: false})
}, this.timer*1000)
}
当我尝试运行它时,所有通知都会立即发送,并且我很难理解如何以不同的方式进行操作。
解决方案
欢迎来到 SO!这是因为 setTimeout 函数是非阻塞的,并且会立即返回,因此循环将非常快速地设置所有计时器,并且所有计时器几乎同时触发,因此您看不到差异。如果您想延迟发送通知,您可以在循环中添加一些延迟,如下所示:
const timer = ms => new Promise(res => setTimeout(res, ms))
async function sendAllNotifications () { // We need to wrap the loop into an async function for this to work
for (let i of this.times) {
this.localNotification()
await timer(this.timer*1000); // then the created Promise can be awaited
}
}
sendAllNotifications();
您的 localNotification 函数将变为:
localNotification() {
this.date = new Date()
this.localNotifications.schedule({
text: "Hey it's time to take a picture",
trigger: {at: new Date(new Date().getTime())},
led: 'FF0000',
sound: 'file:/storage/emulated/0/media/audio/notifications/CwtChime.ogg'
})
this.notificationList.unshift({Title: "Time to take a picture", Body: "Hey, it's been a week since you took a picture, please take one", Reminder: true, Time: `${this.date.toLocaleString()}`, Seen: false})
}
推荐阅读
- javascript - 在反应中制作一个列表组件并使每个列表项成为切换项,以便单击时在其下方显示图片
- python - 当不确定它包含哪些属性时,将元素从解析的 XML 添加到 dict 的更好方法
- python - 导入后在 exec 中进行测试的列表理解在 python3 中不起作用
- xml - 如何忽略节点中的特殊字符?
- html - 使用 httpclient 发布一个奇怪的表单
- java - 使用递归(void 方法)打印子字符串
- java - 如何使用 API 和 Glide 来查看信息何时丢失
- java - Spring Boot应用程序发出的日志中的一组值是什么意思?
- php - Wordpress - 为用户显示特定用户角色的简码
- c# - 使用 Linq to Sql 从 2 个不同的 SQL 表中检索数据到 datagridview