Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTimeout does not work

I want to load an OWL file before executing other (visualisation-)scripts. To do this I tried everything from

$(document).ready

to

function visualize (file) {
if (!file)
    {setTimeout(visualize(file), 2000)}
else
    {jQuery(function($){visFeaturePool.init(file)})}}

I think it has to be possible with the setTimeout but that isn't working. I throws the error: Uncaught RangeError: Maximum call stack size exceeded, so it doesn't wait, it just recalls the visualize function untill the stack is full.

Does anybody know what I am doing wrong? Thanks!

like image 716
JasperTack Avatar asked Dec 04 '11 14:12

JasperTack


People also ask

Why setTimeout is not working in JS?

This is due to when a function is executed as a parameter to setTimeout , the execution context is different to the execution context of the function! Now this will print out undefined because the this keyword is executed in the context of the setTimeout function and is therefore not defined.

What can I use instead of setTimeout?

later.

What happens when setTimeout is 0?

setTimeout(fn, 0) !== 0ms. When working with timers, setting a delay of 0, does not equate to 0ms or “instant” execution. This longer than expected delay may occur because the OS/browser/system is busy with other tasks or because the timer has been throttled.

Does setTimeout run immediately?

Next, you can pass the milliseconds parameter, which will be the amount of time JavaScript will wait before executing the code. If you omit the second parameter, then setTimeout() will immediately execute the passed function without waiting at all.


2 Answers

Instead of

// #1
setTimeout(visualize(file), 2000);

you want

// #2
setTimeout(function() {
    visualize(file);
}, 2000);

or on modern browsers, you can provide arguments to pass to the function after the delay:

// #3
setTimeout(visualize, 2000, file);

Those three explained:

  1. (As SLaks mentions) This calls visualize immediately, and then passes its return value into setTimeout (and since visualize calls itself, it keeps calling itself recursively and you end up with a stack overflow error).
  2. This passes a function reference into setTimeout that, when called, will call visualize and pass it the file argument (with its value as it is then). The function we're passing into setTimeout will have access to the file argument, even though your code has run and returned, because that function is a closure over the context in which it was created, which includes file. More: Closures are not complicated Note that the file variable's value is read as of when the timer fires, not when you set it up.
  3. This passes the visualize function reference into setTimeout (note we don't have () or (file) after it) and also passes file into setTimeout, using its value as of when you set up the call. Later, in modern environments, setTimeout will pass that on to the function when calling it later.

There's an important difference between #2 and #3: With #2, if file is changed between when setTimeout is called and the timer expires, visualize will see file's new value. With #3, though, it won't. Both have their uses. Here's an example of that difference:

let file = 1;

// #2, using "file" when the timer fires, not when you set it up
setTimeout(function() { visualize(file); }, 2000); // Shows 2

// #3, using "file" right away when setting up the timer
setTimeout(visualize, 2000, file); // Shows 1

file = 2;

function visualize(value) {
    console.log(value);
}

If you needed #3's behavior of immediately reading file (rather than waiting until the timer fires) in an environment that didn't support extra arguments to setTimeout, you could do this:

// #4 (for environments that don't support #3)
setTimeout(visualize.bind(null, file), 2000);
like image 68
T.J. Crowder Avatar answered Sep 20 '22 23:09

T.J. Crowder


setTimeout(visualize(file), 2000) calls visualize immediately and passes its result to setTimeout, just like any other function call.

like image 23
SLaks Avatar answered Sep 21 '22 23:09

SLaks