English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

setTimeout() method in JavaScript

 setTimeout()

This is one of many timing events. Thewindowobject allows code to be executed at a specified time interval. The object providesSetTimeout()to execute a function after a certain period of time. It usestwo parametersas a parameter. One isfunction, another isTime, thisTimespecifies the time interval after which the function should be executed.

Syntax

window.setTimeout(function, milliseconds);

Example1

In the following example, the time passed to the setTimeout() function is2seconds. Therefore, the function will execute and display the output after two seconds, as shown in the figure.

<html>
<body>
<p>wait 2 seconds.</p>
<script>
   setTimeout(function(){
      document.write("www.oldtoolbag.com is a great IT technology learning platform");
   }, 2000);
</script>
</body>
</html>

Output Result

wait 2 seconds
www.oldtoolbag.com is a great IT technology learning platform

Example2

In the following example, the time passed to the setTimeout() function is3Seconds. Therefore, the function will execute and display the output after three seconds, as shown in the figure.

<html>
<body>
<p id="time"></p>
<script>
   setTimeout(function(){
      document.getElementById("time").innerHTML = "www.oldtoolbag.com is a great IT technology learning platform";
   }, 3000);
</script>
</body>
</html>

Output Result

www.oldtoolbag.com is a great IT technology learning platform