简体   繁体   中英

Load javascript after 5 seconds after page has loaded?

I have tried the following:

<body id="myBody" onload = "setTimeout('a()', 5000)" / >

Is this the correct method? The reason why I want to do this is because I have my entire website animating in (such as fade ins) on page load. Having my javascript only makes the animation unsmooth.

Any feedback appreciated.

This code will work. Just set your time in milliseconds and write your JS code on loadAfterTime function:

<script>
 window.onload = function(){
   setTimeout(loadAfterTime, 5000)
};


function loadAfterTime() { 
// code you need to execute goes here. 
}
</script>
window.addEventListener("load", function () {
    animation();
    setTimeout(otherOperation, 5000);
}, false);

function animation() {}
function otherOperation() {}

maybe you can use code like this

<body id="myBody" onload = "setTimeout(a, 5000)">
尝试这个

if you are using jQuery your animation has a callback that you can use to delay the firing of all other javascript events like so:

$( window ).on( 'load', function () {

    $( '.someElements' ).animate({

        // properties to animate
    }, 300, function () {

        initScripts();
    });
});

function initScripts () {
  // call all other functions here
}

The Browser is always load all of your script if any when you open the web page. So It depend on when you call your javascript functions. Try this:

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

both of them make sure that all of element on your page have been loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM