[ACCEPTED]-How to delay calling of javascript function?-dom-events
setTimeout
is compatible with all browsers since 1996. You 7 should avoid the evaluation of "functionName()" and 6 instead do:
setTimeout(functionName,5000)
UPDATE: If you initially expect a variable 5 passed to the function and none when in 4 the timeout, you need to do this instead:
setTimeout(function() { functionName() },5000)
However 3 you are calling the onload
incorrectly, so you 2 need to do either this:
window.addEventListener("load",function() {
// your stuff
}
or the simpler
window.onload=function() {
// your stuff
}
or, since 1 you are using jQuery, this:
$(document).ready(function() {
// your stuff
});
or just this:
$(function() {
// your stuff
});
If you want to be 100% sure that it's when 4 the page ACTUALLY loads, use:
$(window).load(function(){
//After EVERYTHING loads, including images.
})
The other's 3 solution, onload
works, but it loads once the 2 DOM is ready, but not when the window is 1 actually finished loading.
If you're going to be using jQuery then 2 it's preferable to attach an event to the 1 document ready event using one of the following:
$(document).ready(callback);
or
$(document).ready(function () { /* do stuff */ });
or
$(callback);
or
$(function () { /* do stuff */ });
$(document).ready(function(){
//Code goes here
});
or old style
<body onload="myFunction()">
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.