Home » Javascript » I am getting [Violation] 'setInterval' handler took N ms warning in chrome dev and after that application get freezed

I am getting [Violation] 'setInterval' handler took N ms warning in chrome dev and after that application get freezed

Posted by: admin June 30, 2018 Leave a comment

Questions:

On running a setInterval call I am getting this issue.
[Violation] ‘setInterval’ handler took ms
In my understanding we get this issue when we write a code inside setInterval which takes more time to execute. My problem is how to solve this issue if there is no way I can alter the code running inside setInterval.

let cardInterval = setInterval(() => {
    if (self.filterObject != undefined) {
      self.getFilteredListOfTurbine(self.globalFilterService.getUpdatedTurbineListWithOperatingMode(self.listOfTurbine)).then((response) => {
        self.cardData.turbines = response;
        self.cardData.recalculate();
        observable.next(self.cardData);
      })
    } else {
      self.cardData.turbines = self.globalFilterService.getUpdatedTurbineListWithOperatingMode(self.listOfTurbine);
      self.cardData.recalculate();
      observable.next(self.cardData);
    }
  }, 1000);

The above code works fine when self.filterObject is undefined but when its not It need to do a extra filtering which is in self.getFilteredListOfTurbine.

  getFilteredListOfTurbine(listOfTurbine): Promise<Turbine[]> {
return new Promise((resolve) => {
  if (this.filterObject == undefined) {
    return listOfTurbine;
  }
  let self = this;
  let listOfFilteredTurbine = new Array<Turbine>();
  listOfTurbine.forEach((turbine, i) => {
    if (self.filterObject.wf.includes(turbine.wfoid) && self.filterObject.tu.includes(turbine.turbineserialnumber) && self.filterObject.tt.includes(turbine.hwtype) && self.filterObject.om.includes(turbine.turbineoperatingmode)) {
      if (self.filterObject.ro && self.filterObject.ro.length > 0) {
        let addTurbine = this.applyRulesOnTurbine(turbine, self.getRuleObject());
        if (addTurbine) {
          listOfFilteredTurbine.push(turbine);
        }
      }
      else {
        listOfFilteredTurbine.push(turbine);
      }
    }
    if (i == listOfTurbine.length - 1) {
      return resolve(listOfFilteredTurbine);
    }
  })
})

}

I dont feel I could change the logic of filtering here. Please help me in fixing this violation issue.

Thanks

Answers: