0

First example (works as expected):

let timer = function (i) {
  setTimeout(() => {
      console.log(i);
  }, i * 1000) // logging every second
};

for (let i = 0; i < 5000; ++i) {
    timer(i)
}
This code works good, in console i can see logging for every second.

Now let's change delay of timeout to 1 hour (1000 * 60 * 60)

 let timer = function (i) {
   setTimeout(() => {
       console.log(i);
   }, i * 1000 * 60 * 60) // logging every hour
};
    
for (let i = 0; i < 5000; ++i) {
   timer(i)
}

This code doesn't work as i'm expect it. I expected logging for every hour. You can try to run it by yourself.

oboshto
  • 3,478
  • 4
  • 25
  • 24
  • 1
    `This code doesn't work as i'm expect it` => Please update your question and specify what does happen and what you are expecting to happen. We should not have to run your code to get the first and guess at the second. – Igor Mar 27 '17 at 09:42
  • " I expected logging for every hour" — And what does happen? – Quentin Mar 27 '17 at 09:44
  • "You can try to run it by yourself." — And then wait for five thousand hours for it to finish? – Quentin Mar 27 '17 at 09:44
  • @Quentin use "Run code snippet". Logging 5k times for ~ 5 seconds – oboshto Mar 27 '17 at 09:46

0 Answers0