Inverval set when $on is set

Asked

Viewed 34 times

2

Guys, I’m getting here to do an interval rsrs.

$rootScope.$on("timer", updateApanha());

 var arquivoJSON = (JSON.parse(window.sessionStorage.getItem('timeUpdate'))*1000);

console.log(tempoDeAtualizacao);

function updateApanha(){
setInterval(getApanha, tempoDeAtualizacao);
console.log("get Apanha " +tempoDeAtualizacao);
   }

The point is that the interval is set to Undefined. And because of that he keeps making the requisition all the time. What I wanted was that only when it was called $on that was set the interval to be able to call the function.

2 answers

2


Try removing parentheses from the function updateApanha, so it will only be assigned to the event timer and unenforced.

$rootScope.$on("timer", updateApanha);
  • That way solved my problem. Just assign the function to the event.Ty

1

The line

$rootScope.$on("timer", updateApanha());

Means: When the message timer is issued, execute the result of function updateApanha.

This makes it UpdateApanha is executed at least once, and immediately.

Browser other questions tagged

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