0

I am building an ionic app and within the constructor of component I have a loop that iterate through all the items. Within the loop I have written setInterval that calls a function from within as shown below.

var thisScope = this;
for (let i = 0; i < this.schedules.length; i++) {
    (function() {
      thisScope.showTime[i] = new Date();
      thisScope.showTime[i].setHours(0, 0, 0);
      thisScope.schedules[i].afh.timerIncDec = '00:00:00';
      if (!thisScope.timerFinish || AfhListviewComponent.timerInSeconds[i] !== 0 || thisScope.schedules[i].afh.timerIncDec != '00:00:00') {
        thisScope.timerId = setInterval(() => {          
            thisScope.timerTick(thisScope.schedules[i]);           
        }, 1000);
      } else if (thisScope.timerFinish || AfhListviewComponent.timerInSeconds[i] === 0 || thisScope.schedules[i].afh.timerIncDec === '00:00:00') {
        clearInterval(thisScope.timerId);
      }
    })();
  }

Here I want to set the timer (its calculations is being done within timerTick function). Here the problem I am facing is setInterval that is being overlapped and its speed gets increased less than 1000. Each item of loop should maintain its own instance of setInterval. The variable that is being used as increment and decrement is timerInSeconds that is static written within same component.

Here the 1000 value is being overlapped by other setIntervals within same loop or same loop having more schedules.

Umair Jameel
  • 1,573
  • 3
  • 29
  • 54

0 Answers0