How to delay calling of javascript function?

后端 未结 5 2055
野性不改
野性不改 2020-11-29 06:09

I\'m new to JavaScript.

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

I tried using

5条回答
  •  醉梦人生
    2020-11-29 06:32

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

    setTimeout(functionName,5000)
    

    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)
    

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

    window.addEventListener("load",function() {
      // your stuff
    }
    

    or the simpler

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

    or, since you are using jQuery, this:

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

    or just this:

    $(function() {
        // your stuff
    });
    

提交回复
热议问题