I have the following code in my constructor:
constructor(){
for (let i = 0; i < 90; i++) {
setTimeout(() => {
this.counter = this.counter - 1;
}, 1000)
}
}
What I actually want is to display a number which counts down 90 seconds. Right now it counts down from 90 to 0 immediately
You can use
setInterval
instead to make the function be called every 1 second until the counter reaches 0:Or if you want something that looks like a
for
and usessetTimeout
you could useasync/await
and Promisses (admittedly this might be overkill for this simple example):