-4

why has this been associated with the above? I read through those answers after understanding the issue from answers provided below and there's nothing in the above that would have generated any insight.

html doc

let tester = 0;
    setInterval(iterateCounter(), 1000);
    
    function iterateCounter(){
      ++ tester;
      console.log(tester);
    }
    

this is probably a horrible question but i've checked MDN, JSinfo, and perused stack for about 20 minutes on top of that. Why is this code snippet running once and then not repeating?

  • 1
    Plenty of dupes on this.... () executes it and what is returned is assigned to the interval. In your case you have `setInterval(undefined, 1000);` – epascarello Aug 26 '20 at 20:51
  • @epascarello non-specific and unhelpful given A) you didn't reference anything specifically B) the question was already answered C) you offered a code snippet that only makes sense to someone who understood the problem a priori. – cooper kennelly Aug 26 '20 at 21:00
  • And your selected answer does NOTHING to explain why. – epascarello Aug 26 '20 at 21:29
  • 1
    @epascarello pragmatic > pedantic, the answer below it explains why perfectly explicitly. – cooper kennelly Aug 27 '20 at 03:16

2 Answers2

2

You should only pass the function name instead of calling it:

let tester = 0;
setInterval(iterateCounter, 1000);
    
function iterateCounter(){
      ++ tester;
      console.log(tester);
}

For more info, you can check the docs.

Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
0

setInterval in its first argument accepts function itself, not what function returns. To make it work simply remove scopes after iterateCounter

let tester = 0;
    setInterval(iterateCounter, 1000);
    
    function iterateCounter(){
      ++ tester;
      console.log(tester);
    }
    
Artem
  • 102
  • 7