Forum Moderators: open

Message Too Old, No Replies

Using setTimeOut

Pretty confused about what I even need to ask, but here goes...

         

MatthewHSE

4:36 pm on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm toying around with a pre-made JavaScript I use on my site. However, I'd like to get it so that one function doesn't start until after the page has completed loading. I know I can do that with setTimeOut, but the question is, how? I tried putting this in my external JavaScript:

window.onload = setTimeOut(1000);

But it seemed to have no effect. Here's something else I tried, which also had no effect:

window.onload = function(myFunction) {
setTimeOut(10000);
}

I also tried this, in my actual HTML document:

<body onload="setTimeOut(1000)">

Also with no effect.

So what am I missing here? I've read through a couple tutorials but couldn't find any solution. Would appreciate any input.

Thanks,

Matthew

john_k

4:53 pm on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



setTimeout is a function of the window object. The two parameters it takes are the code to be executed and the number of miliseconds to wait before executing that code.

So something like this should work:

<body onload="setTimeout('myFunction(12)',1000);">

When the onload event fires, it will set a timer for 1 second. After one second, the timer will execute the javascript in the first parameter. In this example it will call myFunction and pass a parameter of 12.

Remember - the onLoad event is supposed to be fired after the page is finished loading. So it may be sufficient to put your code in the onLoad of the body tag. If there are critical external files (such as other javascript, images, etc.) then it is possible that they won't be done loading when the onLoad fires. In this case setTimeout will definitely help you.