From 98e959346c661bedf4efb5d6c7717e996c8842c1 Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Mon, 23 May 2022 15:44:34 +0200 Subject: [PATCH] Reduce time complexity of alarms calculation --- api/js/etemplate/et2_widget_countdown.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/api/js/etemplate/et2_widget_countdown.ts b/api/js/etemplate/et2_widget_countdown.ts index d4c150f1e4..c7458fed3f 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 = {