I have javascript
function and that should be called after 3 seconds of complete page load. I know about setIntervel
but it repeat execution after certain time interval. I want it to execute once. Is it even possible?
Asked
Active
Viewed 1.9k times
7

Imad
- 7,126
- 12
- 55
- 112
-
Possible duplicate of [How to delay calling of javascript function?](https://stackoverflow.com/questions/5570017/how-to-delay-calling-of-javascript-function) – Vadzim Nov 01 '17 at 01:15
3 Answers
11
The onload event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading, After
onload
you can usesetTimeout
to delay your function execution..
var myFunc = function() {
alert('After 3 seconds of page load!');
}
window.onload = function() {
setTimeout(myFunc, 3000);
}

Rayon
- 36,219
- 4
- 49
- 76
9
Use setTimeout
instead, as it is called only once after the pause:
setTimeout(myFunc, 3000);

Rory McCrossan
- 331,213
- 40
- 305
- 339