JavaScript setTimeout() Tidbit - Don't pass parameters!

Greetings!

Here’s an absolutely ANNOYING side effect of using setTimeout() in JavaScript that I did not know, and absolutely nobody mentions - not even famous JavaScript book authors - that I found tucked away in an obscure post somewhere that I don’t remember.

The general syntax for a setTimeout() statement is like this:

var id = setTimeout(fn. delay);

Where “fn” is a function to be called after the delay ends and “delay” is the amount of time, in milliseconds, (1000ms = 1 second), that the timer should wait before running the function.

Nobody, and I mean NOBODY except for one obscure author mentions anything about the characteristics of the functional requirement.

Here’s the “gotcha”:
If the function you’re passing to the timer has ANY parameters, the timer returns immediately after pushing the function into the queue.  Actually there’s a better, more technical explanation, but I forgot what it was.

For example:
var id = setTimeout(fn. delay);  waits “delay” milliseconds and then executes “fn”.

However:
var id = setTimeout(fn(parameters). delay);  returns immediately after pushing “fn(parameters)” on the execution queue which, essentially, causes it to execute immediately.

Why?  Only the Gods of JavaScript know - and it’s a pain!

I noticed that when I run

var id = setTimeout(requestAnimationFrame(animation), 1000);

There was absolutely no delay whatsoever.

However if I run this:

function dothis() {
   requestAnimationFrame(animation);
}
[more code]

setTimeout(dothis, 1000);

the execution of “requestAnimationFrame” is delayed by one-second intervals as it should be.

Annoying and (almost) totally un-documented.

Grrrrr!   :face_with_symbols_over_mouth:

P. S.
I don’t know if this is also the case for setInterval() or not.

1 Like

There are many land mines waiting for JavaScript initiates - understanding the eval loop, understanding callbacks and context, understanding closures, debugging with anonymous functions, and actually debugging period. They each have “rites of passage” or scars for those of us with little patience for pedagogy.

1 Like

It’s not so much a lack of patience, it’s a lack of time.  In my past, I would take pleasure in curling up with a book on [pick language or technical topic] and happily read it from cover-to-cover; anticipating the joy of experimenting with the new-found knowledge once I’m done.

Nowadays, I have two granddaughters to chase after and keep out of trouble, a wife who is firmly convinced that I’m always chasing the wrong goal based on her whim-of-the-moment, a son and daughter-in-law who are absolutely convinced that the two of us are idiots, along with the idiocies of living in a foreign country.

My eldest granddaughter asked me once:
“Why do you always stay up at night until 4:00?”
“Because the only time I can do the things I want to do is after everyone else has gone to sleep!”

1 Like

using the interval method
var myvar=setinterval(mytimer,1000);

and create mytimer function and put the time

1 Like

 

  1. What do I do with the “myvar” variable?  Throw it away?

  2. Why do I have to put the time in the “mytimer” function?  Isn’t that specified by the second parameter of the setinterval statement?