Calling a function in JavaScript a few seconds after page load

by on January 28, 2006

Q: How do you call a function, using JavaScript, in your web browser a few seconds after a page loads? We would like to display a portion of a page for about 5 seconds and then minimize the section after that.

A: To call a function once a page load you want to use the onLoad method in the body tag of your html. To do the delayed call we will need to use the JavaScript function window.setTimeout( function, time ).

The two arguments that the function takes are the function you want to call and the time in milliseconds you want it to wait before calling that function.

Putting both together we would use the code below to achieve what this user was trying to do.

<body onLoad=”window.setTimeout(‘my_function()’, 2000);”>


One Response to “Calling a function in JavaScript a few seconds after page load”
  1.  

    How to display a pop-up on the taskbar if the function of the pop-up was created in IE?