0
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.

Slom
  • 61
  • 5

1 Answers1

0

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);
}
charlietfl
  • 170,828
  • 13
  • 121
  • 150