- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is setInterval() method in JavaScript?
setInterval()
This is one of the many timing events. The window object allows code to execute at every certain interval of time. This object has provided SetInterval() to repeat a function for every certain amount of time. It takes two parameters as arguments. One is the function and the other is the time that specifies the interval after which the function should be repeated.
syntax
window.setInterval(function, milliseconds, param1, param2, ...));
This method can also take other parameters and can add it to the function.
Example-1
In the following example, setInterval() method is defined and a time interval of 3000 millisecs or 3 seconds is declared. Therefore the function provided inside it will repeat for every 3 secs. The result, for a time span of 9 secs, is showed in the output.
<html> <body> <script> setInterval(function(){ document.write("Tutorix </br>"); }, 3000); </script> </body> </html>
Output
Tutorix Tutorix Tutorix
Example-2
In the following example, setInterval() method is defined and a time interval of 2000 millisecs is declared. Therefore the function provided inside it will repeat for every 2 secs. The result is showed in the output.
<html> <body> <script> setInterval(function(){ document.write("Hello </br>"); }, 2000); </script> </body> </html>
Output
Hello Hello