The window object allows the execution of code at specified time intervals. To delay a function call, you can use setTimeout() function. This function will Execute a function, after waiting for a specified number of milliseconds.
JavaScript delay function Example code
HTML example code where you have to click a button and wait 1 second and the page will alert “1 second! Alert”:
<!DOCTYPE html>
<html>
<body>
<button onclick="timeFunction()">Click Here</button>
<script>
function timeFunction() {
setTimeout(function(){ alert(" 1 seconds! Alert"); }, 1000);
}
</script>
<p>Wait for 1 seconds after click...</p>
</body>
</html>
Output:
Do comment if you have any doubts or suggestions on this topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version