Java Script | setTimeout() & setInterval() Method
setTimeout() method
The setTimeout() method executes a function, after waiting a specified number of milliseconds.
Syntax:
window.setTimeout(function, milliseconds);
Parameter: There are two parameter that accepted by this method
- function : first parameter is a function to be executed
- milliseconds : indicates the number of milliseconds before execution takes place.
For example, we want an alert box to pop up, 2 seconds after the user presses the click me button.
Example:
<!DOCTYPE html> <html> <head> <title> HTML | DOM Window setTimeout() method </title> </head> <body> <button onclick="setTimeout(gfg, 2000);"> Press me </button> <script> function gfg() { alert('Welcome to GeeksforGeeks'); } </script> </body> </html> |
Output:
As soon as the user presses the “press me” button, then after a pause of 2 seconds this message alert
box will pop up.

setInterval() Method
The setInterval() method repeats a given function at every given time-interval.
Syntax:
window.setInterval(function, milliseconds);
Parameter: There are two parameter that accepted by this method
- function : first parameter is the function to be executed
- milliseconds :indicates the length of the time-interval between each execution.
Example:
<!DOCTYPE html> <html> <head> <title> HTML | DOM Window setTimeout() method </title> </head> <body> <p>I will say hi many times</p> <p id="GFG"></p> <script> var myVar = setInterval(myTimer, 1000); function myTimer() { document.getElementById("GFG").innerHTML += "<p>Hi</p>"; } </script> </body> </html> |
Output:
After every second a new “hi” message will be displayed.

Then:

Supported Browser: The browser supported by setTimeout() & setInterval() Method are listed nelow:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- Run Python script from Node.js using child process spawn() method
- JavaScript : The Awesome Script
- Amazon auto signup script
- jQuery | add() method with Example
- JavaScript | compile() Method
- JavaScript | Array.from() method
- JavaScript | Array map() Method
- JavaScript | Array from() Method
- JavaScript | getTime() Method
- JavaScript | Sort() method
- jQuery | not() method with Examples
- JavaScript | exec() Method
- preventDefault() Event Method
- JavaScript | Replace() Method
- Javascript | Window prompt() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



