0

I have a counter number incrementing from 0 to a given number(percentage). I want to increment it in steps of one after every few miliseconds.

let count = 0;
let percentage = 99;
let intervalID = setInterval(function (){       
    count++;
    console.log(count)       // prints values from 1 through 99
    if (count === percentage) {
         clearInterval(intervalID);
    }
},200);

But the count on the view is not updating

1

In JavaScript, sync and async lines are handled differently. In the JS event loop first sync events are handeled, after which on a diffrent thread, async events are handled.

It becomes important to pass context in JavaScript.

Use .bind(this) for your setInterval anonymous function so that context will be maintained.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.