0

    setTimeout(()=> alert('hi'), 3000); 
    setTimeout(()=> alert('hello'), 5000);

I am really struggling hard understanding setTimeout method. In the above code, the first statement works correctly meaning, it displays hi after 3 seconds.

But the next statement doesn't delay by 5seconds. Why? The second output is shown immediately after the first one without any dealy. Please explain the behaviour. Refer me some article to understand in detail.

ezio4df
  • 3,541
  • 6
  • 16
  • 31
Thelostcause
  • 113
  • 7

1 Answers1

0

Actually it is working correct:

console.log('start now:',new Date().toISOString());
setTimeout(()=> console.log('hi now:',new Date().toISOString()), 3000); // 3 seconds after starts
setTimeout(()=> console.log('hello now:', new Date().toISOString()), 5000); // 5 seconds after starts, 2 seconds after hi.
Vahid Alimohamadi
  • 4,900
  • 2
  • 22
  • 37