Reduce time complexity of alarms calculation

This commit is contained in:
Hadi Nategh 2022-05-23 15:53:09 +02:00
parent 4d7dffeb24
commit bc63d06f16
2 changed files with 27 additions and 12 deletions

View File

@ -86,11 +86,18 @@ var et2_countdown = /** @class */ (function (_super) {
var distance = this.time.getTime() - now.getTime(); var distance = this.time.getTime() - now.getTime();
if (distance < 0) if (distance < 0)
return 0; return 0;
var alarms = Array.isArray(this.options.alarm) ? this.options.alarm : [this.options.alarm]; var alarms = [];
for (var i = 0; i <= alarms.length; i++) { if (Array.isArray(this.options.alarm)) {
if (alarms[i] > 0 && alarms[i] == Math.floor(distance / 1000) && typeof this.onAlarm == 'function') { alarms = this.options.alarm;
this.onAlarm(); }
} 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 = { var values = {
days: Math.floor(distance / (1000 * 60 * 60 * 24)), days: Math.floor(distance / (1000 * 60 * 60 * 24)),

View File

@ -121,14 +121,22 @@ export class et2_countdown extends et2_valueWidget {
if (distance < 0) return 0; if (distance < 0) return 0;
let alarms = Array.isArray(this.options.alarm) ? this.options.alarm : [this.options.alarm]; let alarms = [];
if (Array.isArray(this.options.alarm))
for (let i=0;i<=alarms.length;i++)
{ {
if (alarms[i] > 0 && alarms[i] == Math.floor(distance/1000) && typeof this.onAlarm == 'function') alarms = this.options.alarm;
{ }
this.onAlarm(); 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 = { let values = {