sams5817 sams5817 - 6 months ago 3x
Javascript Question

How to delay calling of javascript function?

I'm new in javascript.

I would like to call JavaScript / jQuery function after the page load in aspx page.

I tried using

<form onload="function()">
window.load = function(){}
, but the JavaScript is still trigger before some of the content fully loaded.

Is it possible to call during
in aspx page and not in code behind, so that I can delay the JavaScript function ?

I tried
to solve the problem.

seem like is not compatible with some browser, e.g: caused looping in Google Chrome.

Appreciate if you could provide me the solution, thank you.


setTimeout is compatible with all browsers since 1996. You should avoid the evaluation of "functionName()" and instead do:


UPDATE: If you initially expect a variable passed to the function and none when in the timeout, you need to do this instead:

setTimeout(function() { functionName() },5000)

because setTimeout will pass the timeout ID to the function, if it is called like in the first example.

However you are calling the onload incorrectly, so you need to do either this:

window.onload=function() {
  // your stuff

or, since you are using jQuery, this:

$(document).ready(function() {
    // your stuff

or just this:

$(function() {
    // your stuff