for (var i = 0; i <= 100; i++) {
setTimeout(function() {
celsiusToFahrenheit(i);
}, 1000);
}
I'd like to make the celsius function run every second for 100 times but somehow it does not work and I have no idea why. Pls halp.
for (var i = 0; i <= 100; i++) {
setTimeout(function() {
celsiusToFahrenheit(i);
}, 1000);
}
I'd like to make the celsius function run every second for 100 times but somehow it does not work and I have no idea why. Pls halp.
That loop will create 100 setTimeout that all run at virtually the same time since the loop will complete in a few milliseconds.
Multiply the duration by i
to increment it so the durations will be multiples of 1000
.
You need to use let
instead of var
to block scope i
for (let i = 0; i <= 100; i++) {
setTimeout(function() {
celsiusToFahrenheit(i);
}, 1000 * i);
}