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
  • 36,946
  • 82
  • 271
  • 487

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
  • 76,500
  • 11
  • 62
  • 80