diff --git a/api/js/etemplate/et2_widget_countdown.js b/api/js/etemplate/et2_widget_countdown.js index 6ca4ce28e7..8bd36daa22 100644 --- a/api/js/etemplate/et2_widget_countdown.js +++ b/api/js/etemplate/et2_widget_countdown.js @@ -86,11 +86,18 @@ var et2_countdown = /** @class */ (function (_super) { var distance = this.time.getTime() - now.getTime(); if (distance < 0) return 0; - var alarms = Array.isArray(this.options.alarm) ? this.options.alarm : [this.options.alarm]; - for (var i = 0; i <= alarms.length; i++) { - if (alarms[i] > 0 && alarms[i] == Math.floor(distance / 1000) && typeof this.onAlarm == 'function') { - this.onAlarm(); - } + var alarms = []; + if (Array.isArray(this.options.alarm)) { + alarms = this.options.alarm; + } + else { + alarms[this.options.alarm] = this.options.alarm; + } + // alarm values should be set as array index to reduce its time complexity from O(n) to O(1) + // otherwise the execution time might be more than a second which would cause timer being delayed + if (alarms[Math.floor(distance / 1000)] && typeof this.onAlarm == 'function') { + console.log('alarm is called'); + this.onAlarm(); } var values = { days: Math.floor(distance / (1000 * 60 * 60 * 24)), diff --git a/api/js/etemplate/et2_widget_countdown.ts b/api/js/etemplate/et2_widget_countdown.ts index e795a67921..8c2652688f 100644 --- a/api/js/etemplate/et2_widget_countdown.ts +++ b/api/js/etemplate/et2_widget_countdown.ts @@ -121,14 +121,22 @@ export class et2_countdown extends et2_valueWidget { if (distance < 0) return 0; - let alarms = Array.isArray(this.options.alarm) ? this.options.alarm : [this.options.alarm]; - - for (let i=0;i<=alarms.length;i++) + let alarms = []; + if (Array.isArray(this.options.alarm)) { - if (alarms[i] > 0 && alarms[i] == Math.floor(distance/1000) && typeof this.onAlarm == 'function') - { - this.onAlarm(); - } + alarms = this.options.alarm; + } + else + { + alarms[this.options.alarm] = this.options.alarm; + } + + // alarm values should be set as array index to reduce its time complexity from O(n) to O(1) + // otherwise the execution time might be more than a second which would cause timer being delayed + if (alarms[Math.floor(distance/1000)] && typeof this.onAlarm == 'function') + { + console.log('alarm is called') + this.onAlarm(); } let values = {