Set Inverval when $ on is set

2

Well, I'm going to be here for an interview.

$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 it is set to undefined. And on account of that he is making the requisition all the time. What I wanted was that only when it was called $ on that the interval was set to be able to call the function.

    
asked by anonymous 07.02.2017 / 14:53

2 answers

2

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

$rootScope.$on("timer", updateApanha);
    
07.02.2017 / 15:18
1

The line

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

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

This causes UpdateApanha to be executed at least once, and immediately.

    
07.02.2017 / 15:24