-1

Why does declaring var timer = setTimeout(etc.) automatically execute the setTimeout() function? And how would one pass a setTimeout() function as a variable without having the setTimeout() automatically execute?

var do_this = function(params){console.log("executes automatically, but function is only declared")};
var delay = 50;
var timeoutID = setTimeout(do_this(), delay) //executes automatically
Shaun
  • 1
  • 3

1 Answers1

-1

Functions such as setTimeout require a callback-function that can be called the moment the timer expires. When you just provide a function, it is called immediately because it is never added to the callback-queue.

Thus, you should pass it a callback-function:

setTimeout( () => ... )
Bart Barnard
  • 1,128
  • 8
  • 17