Execute JS function after some time loading the page

I have a function javascript

and it should be called after 3 seconds of full page load. I know about setIntervel

, but it repeats after a certain interval of time. I want it to be executed once. Is it possible?

+3


source to share


3 answers


Use setTimeout

this instead as it only gets called once after a pause:



setTimeout(myFunc, 3000);

      

+3


source


Using setTimeout:

 setTimeout(function(){myfunc()}, 3000);

      



with lambda ..

+1


source


Try the following:

The onload event fires at the end of the document loading process. At this point, all objects in the document are in the DOM, and all images, scripts, links and subframes are finished, after which onload

you can use setTimeout

to delay execution of your function.

var myFunc = function() {
  alert('After 3 seconds of page load!');
}
window.onload = function() {
  setTimeout(myFunc, 3000);
}
      

Run codeHide result


0


source







All Articles