This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

JavaScript SetTimeOut Function


In this tutorial we will learn how to set delay time for function in JavaScript. In JavaScript, the setTimeout() function is used to execute a function after a specified delay in milliseconds. The function is executed once, after the specified delay has elapsed.

Here is the syntax for using setTimeout() function:

setTimeout(function, delay, param1, param2, ...);

The function parameter is the function that will be executed after the delay has elapsed. The delay parameter is the number of milliseconds to wait before executing the function. Optional parameters param1, param2, etc. can be passed as arguments to the function.

Here is an example of using the setTimeout() function:


function setTimeout(name) {
    document.write(`Hello, ${name}!`);
}
setTimeout(setTimeout, 3000, "Rohatash");

In this example, the sayHello() function is executed after a delay of 3000 milliseconds (3 seconds), and the string "Rohatash" is passed as a parameter to the function.

You can also use an anonymous function with setTimeout() to execute a block of code after a delay:

setTimeout(function() {
    document.write("This message will be logged after 10 seconds delay.");
}, 2000);

In this example, an anonymous function is passed as the first parameter to setTimeout(), and it logs a message to the console after a delay of 10000 milliseconds (10 seconds).

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>TutorialsTrend - SetTimeOut Functoin Examples</h2> 
<button onclick="setTimeout()">SetTimeOut Check</button>
<script>
setTimeout(function () {
document.write("This message will be logged after 2 seconds.");
}, 2000);
</script>
</body>
</html>

Output

The following message will show after 10 seconds - "This message will be logged after 2 seconds."

javascript SetTimeOut Function
Prev Next