Serguei Fedorov
Serguei Fedorov

Reputation: 7913

setTimeout() triggering without delay

I have the following code

        Ext.onReady(function () {
            setTimeout(everything(), 30000);
        });

I am trying to wait for EXT.NET to completely finish compiling the page before applying any javascript to the elemtents. This is not a problem in most browsers as $(document).load provides enough of delay. Of course the horrid internet explorer triggers .load prematurely which means I have to put in a hard coded delay in. The above code however, does NOTHING to delay the execution of everything().

Any ideas?

Upvotes: 3

Views: 2085

Answers (3)

James Sumners
James Sumners

Reputation: 14777

As others have posted, you should be writing this instead:

Ext.onReady(function() {
  setTimeout(everything, 30000);
});

What they haven't posted is an explanation. In the above snippet, everything is a reference to a function within the scope that called the Ext.onReady() method. In your question, you are using setTimeout() to execute the result of the everything() function. It's likely that your everything() function returns undefined, so your setTimeout() call never does anything.

Someone suggested using setTimeout("everything()", 30000). This is effectively the same thing as using eval(). Thus, this is not a recommended approach.

Upvotes: 1

brenjt
brenjt

Reputation: 16297

change

setTimeout(everything(), 30000); 

to

setTimeout("everything()", 30000);

or

setTimeout(everything, 30000);

Upvotes: 2

Kevin B
Kevin B

Reputation: 95030

Remove the () from everything

setTimeout(everything, 30000);

By including (), you are telling the browser to immediately execute everything and send it's return value as the callback function to the setTimeout.

Upvotes: 14

Related Questions