Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closure counter inside setInterval

I have a function:

setInterval(function () {
        var counter = 0;
        (function() {
          counter = counter + 1;
          console.log(counter);
          })(counter)
       }, 1000)

Why does not it increment the counter? (instead, it logs 1's). How to make it log ascending numbers? (1, 2, 3, ....)

like image 393
techkuz Avatar asked May 22 '26 20:05

techkuz


2 Answers

  1. You are passing an argument to your anonymous function, but you aren't assigning that argument to a variable. You forgot to put the arguments in the function definition.
  2. You are creating new variables with every iteration instead of sharing the variable between them. You need to turn your logic inside out.

(function(closed_over_counter) {

  setInterval(function() {
    closed_over_counter++;
    console.log(closed_over_counter);
  }, 1000);

})(0);

Since you are using an IIFE instead of a function you can call multiple times, this is pretty pointless. You might as well just declare the variable inside the closure.

(function() {
  var counter = 0;
  setInterval(function() {
    counter++;
    console.log(counter);
  }, 1000);
})();

Or, since Internet Explorer is no longer a concern for most people, dispense with the IIFE and use a block instead.

{
  let counter = 0;
  setInterval(function() {
    counter++;
    console.log(counter);
  }, 1000);
}
like image 59
Quentin Avatar answered May 24 '26 09:05

Quentin


You could use a function which returns a function as closure over counter.

setInterval(function(counter) {
    return function() {
        console.log(++counter);
    };
}(0), 1000);
like image 36
Nina Scholz Avatar answered May 24 '26 10:05

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!