How to do if not repeat the function for anyone who has already reached a number

Asked

Viewed 42 times

0

I make a BD call through the API to see the sales percentage data of the branches of a store, and if the percentage is greater than or equal to 100% I should play a song and change its color to green.

And I use the Schedule (https://www.npmjs.com/package/node-schedule) to reload the content.

Except that every time the content is reloaded it plays again the music for the branch that has already reached the goal, and it is only to play the song only once for the branch, has to play again if another branch reaches the goal.

Method that lists branches, changes color, plays music and percentage of them:

 ListaFiliaisDiaria() {

    this.MetaService.FiliaisMetaDiarias().subscribe(
      data => {
        const response = (data as any)
        this.objeto_retorno = JSON.parse(response._body);

        this.fil.length = 0;
        this.filMetade.length = 0
        this.filNaoAtingida.length = 0

        this.objeto_retorno.forEach(element => {

          this.tots = element.TOTAL
          element.TOTAL = (element.TOTAL * 100).toFixed(3) + '%'
          element.TOTAL = element.TOTAL.toString().replace(".", ",")


          if (this.tots >= this.MetaAtingida) {

            this.audio = new Audio();
            this.audio.src = "../../../assets/music/SupermanCorrect.mp3";
            this.audio.load();
            this.audio.play();

            this.fil.push({
              FI: element.FILIAL,
              porc: element.TOTAL             
            });

            this.mainColor = 'MetaAtingida'          

          }
          else if (this.tots >= this.MetaMetade && this.tots < this.MetaAtingida) {

            this.filMetade.push({
              filialMetade: element.FILIAL,
              porcMetade: element.TOTAL
            });

            this.mainColorTwo = 'MetaOitenta'

          }

          else if (this.tots < this.MetaMetade) {

            this.filNaoAtingida.push({
              FilialNaoAtingida: element.FILIAL,
              MetaNaoAtingida: element.TOTAL
            });
            this.mainColorThree = 'MetaNaoAtingida'

          }

        })

      }
}

and in ngOnInit():

  ngOnInit() {
    schedule.scheduleJob(' 15 * * * * * ', () => {         
      this.ListaFiliaisDiaria()
    });
}

the HTML listing the subsidiaries:

<p [class]="mainColor + ' teste' " *ngFor="let element of fil" id="fi">Filial {{ element.FI }} = {{ element.porc }}</p>

<p [class]="mainColorTwo + ' teste' " *ngFor="let element of filMetade">Filial {{ element.filialMetade }} = {{ element.porcMetade }}</p>

<p [class]="mainColorThree + ' teste '" *ngFor="let element of filNaoAtingida">Filial {{ element.FilialNaoAtingida }} = {{ element.MetaNaoAtingida }}</p>

How can I play just once the music for that branch? Someone can help me?

  • It would be a cleaner code to separate the part that plays the music into another function. Basically you have to compare your old state with the new one and touch only if any that had not already hit the target now has hit.

  • But how will I compare the old with the new?

  • 1

    There are several ways the easiest would be to save the Response result in a property of that component. Ai each new request you compare with this property and at the end arrow it pro value of the new request.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.