0

I have the following code:

    ...
    counters: [0, 0]
    ...
        var limit = [3, 5];
        var intervals = [];
        for (var i in limit) {
            intervals.push(setInterval(function () {
                that.counters.splice(i, 1, that.counters[i] + 1);
                if (that.counters[i] >= limit[i]) {
                    clearInterval(intervals[i]);
                }
            }, 10));
        }

When I write it without the for loop, I get 2 counters: one counts to 3 and a second to 5.

But when I run this code, I have one counter which stops at 0 and a second counter that never stops.

Why this is happening and how can I make it work with a for loop?

alexarsh
  • 5,123
  • 12
  • 42
  • 51

0 Answers0