Javatpoint Logo
Javatpoint Logo

How to call a function repeatedly every 5 seconds in JavaScript

JavaScript is a programming language that allows developers to create dynamic web pages and applications. One of the most important features of JavaScript is its ability to execute code repeatedly, at predefined intervals. In this article, we will discuss how to call a function repeatedly every 5 seconds in JavaScript.

The setInterval() Method

The setInterval() method is a built-in JavaScript function that allows developers to execute a function repeatedly at predetermined intervals.

Syntax of setInterval() Method:

The syntax for setInterval() method is as follows:

Here, the first parameter is the function that has to be run repeatedly, and the second parameter is the time interval in milliseconds. The setInterval() method returns a numeric value that may be utilized to stop the execution of the function.

Calling a Function Repeatedly Every 5 Seconds

Using the setInterval() method, we need to specify a time interval of 5000 milliseconds (5 seconds) in order to call a function repeatedly every 5 seconds. Here is an example code snippet that demonstrates how to call a function repeatedly every 5 seconds in JavaScript:

Output:

Hello World!
Hello World!

Hello World!...

Explanation:

In this code snippet, we have defined a function named myFunction() that simply logs the message "Hello World!" to the console. After that, we use the setInterval() method to call the myFunction() function every 5 seconds.

The setInterval() method will continue to call the myFunction() function every 5 seconds until it is explicitly stopped. We can use the clearInterval() method to stop the execution of setInterval() method. The clearInterval() method takes the numeric value returned by the setInterval() method as its parameter.

In this code snippet, we have stored the numeric value returned by setInterval() method in a variable named intervalID. After that, we use the clearInterval() method to stop the execution of the setInterval() method.

Using Anonymous Functions

In the previous example, we defined a named function named myFunction() and passed it as a parameter to the setInterval() method. Alternatively, we can also define an anonymous function and pass it as a parameter to the setInterval() method. Here is an example code snippet that demonstrates how to use anonymous functions with the setInterval() method:

Output:

Hello World!
Hello World!
Hello World!....

Explanation:

In this code snippet, we have defined an anonymous function and passed it as a parameter to the setInterval() method. The anonymous function simply logs the message "Hello World!" to the console. The setInterval() method will continue to call the anonymous function every 5 seconds until it is explicitly stopped.

Using Arrow Functions

In ES6, a new feature was introduced called arrow functions. Arrow functions provide a concise way to define functions in JavaScript. Here is an example code snippet that demonstrates how to use arrow functions with the setInterval() method:

Output:

Hello World!
Hello World!
Hello World!...

Explanation:

In this code snippet, we have used an arrow function to define the function that has to be run repeatedly. The arrow function simply logs the message "Hello World!" to the console. The setInterval() method will continue to call the arrow function every 5 seconds until it is explicitly stopped.

Passing Arguments to the Function

In this example, we will pass arguments to the function that needs to be executed repeatedly. We can pass arguments to the function by including them in the function call inside the setInterval() method.

Output:

Hello John!
Hello John!
Hello John!...

Explanation:

In this code snippet, we have defined a function named greet() that takes a name parameter and logs a greeting message to the console. After that, we use the setInterval() method to call the greet() function every 5 seconds and pass the name parameter as "John".

Conclusion

In this article, we discussed how to call a function repeatedly every 5 seconds in JavaScript. We learned that the setInterval() method can be used to execute a function repeatedly at a specified time interval. The setInterval() method is a powerful tool in JavaScript that allows developers to execute code repeatedly at a specified time interval. We also learned how to stop the execution of the setInterval() method using the clearInterval() method. Additionally, we discussed how to use anonymous functions and arrow functions with the setInterval() method to define functions in a concise and elegant







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA