0

Why setTimeout triggers right away instead of waiting 10000 milliseconds ? https://jsfiddle.net/gtrw3enf/8/

  function main(a) {
    alert(` ${a}`);
    let timeoutID;
    if (typeof main.once == 'undefined') {
      timeoutID = setInterval(loop, 5000);
    }
    
    async function loop() {
      console.log(a)
    }
  }

  main("test");
  setTimeout(main("test2"), 10000);
user310291
  • 35,095
  • 76
  • 252
  • 458

1 Answers1

1

You're immediately calling main and passing the return value to setTimeout, when the first argument to setTimeout should be the function to execute after the delay.

setTimeout(()=>main("test2"), 10000);
Unmitigated
  • 46,070
  • 7
  • 44
  • 60