代码优化--定时器

This commit is contained in:
joylink_zhaoerwei 2023-10-18 11:17:45 +08:00
parent c221bd6d56
commit 5f1999adfb

View File

@ -35,8 +35,8 @@ watch(
(val) => {
if (val.length) {
const alarmType = val[0].alert_type;
playAlarmMusic(alarmType);
alarm(alarmType);
playAlarmMusic(alarmType);
}
}
);
@ -69,18 +69,14 @@ function playAlarmMusic(type: number) {
}
//3
function timingPlayAlarmMusic(
dialog: DialogChainObject,
timer: string | number | NodeJS.Timeout | undefined
) {
clearTimeout(timer);
timer = setTimeout(() => {
if (dialogInstance.length) {
for (let i = 0; i < dialogInstance.length; i++) {
if (dialogInstance[i].show && dialogInstance[i].dialog == dialog) {
playAlarmMusic(dialogInstance[i].alarmType);
timingPlayAlarmMusic(dialog, timer);
}
function timingPlayAlarmMusic(dialog: DialogChainObject) {
const timerIndex = dialogInstance.findIndex((item) => item.dialog == dialog);
clearTimeout(dialogInstance[timerIndex].timer);
dialogInstance[timerIndex].timer = setTimeout(() => {
for (let i = 0; i < dialogInstance.length; i++) {
if (dialogInstance[i].show && dialogInstance[i].dialog == dialog) {
playAlarmMusic(dialogInstance[i].alarmType);
timingPlayAlarmMusic(dialog);
}
}
}, 180000);
@ -102,13 +98,13 @@ function alarm(alarmType: number) {
dialogInstance[index].show = false;
});
let timer: string | number | NodeJS.Timeout | undefined = undefined;
timingPlayAlarmMusic(dialogInstanceItem, timer);
dialogInstance.push({
dialog: dialogInstanceItem,
show: true,
alarmType,
timer,
});
timingPlayAlarmMusic(dialogInstanceItem);
}
onUnmounted(() => {