How to call a function repeatedly every 5 seconds in JavaScript

JavaScript is a versatile programming language that empowers developers to build interactive web pages and applications. A key feature of JavaScript is its capability to run code multiple times at specified intervals. In this article, we will explore the method for invoking a function consistently every 5 seconds in JavaScript.

The setInterval Method

The setInterval function is a native JavaScript method that enables programmers to invoke a function multiple times at specified time intervals.

Syntax of setInterval Method:

The structure of the setInterval function is outlined below:

Example

setInterval(function, milliseconds);

In this context, the initial parameter refers to the function that needs to be executed multiple times, while the subsequent parameter indicates the time interval specified in milliseconds. The setInterval method produces a numeric identifier that can be used to halt the function's execution.

Calling a Function Repeatedly Every 5 Seconds

By utilizing the setInterval function, we must define a time period of 5000 milliseconds (which equates to 5 seconds) to invoke a specific function at regular intervals of 5 seconds. Below is a sample code excerpt illustrating how to repeatedly execute a function every 5 seconds in JavaScript:

Example

function myFunction() {
  console.log("Hello World!");
}
setInterval(myFunction, 5000);

Output:

Output

Hello World!
Hello World!

Hello World!...

Explanation:

In the following code example, we have created a function called myFunction which outputs the message "Hello World!" to the console. Subsequently, we utilize the setInterval method to invoke the myFunction function every 5 seconds.

The setInterval function will repeatedly invoke the myFunction function every five seconds until it is deliberately halted. To terminate the execution initiated by the setInterval function, we can utilize the clearInterval method. This clearInterval method requires the numeric identifier that is returned from the setInterval function as its argument.

Example

var intervalID = setInterval(myFunction, 5000);
clearInterval(intervalID);

In the following code example, we assign the numeric identifier produced by the setInterval function to a variable called intervalID. Subsequently, we utilize the clearInterval function to halt the ongoing execution initiated by the setInterval method.

Using Anonymous Functions

In the earlier example, we established a named function referred to as myFunction and provided it as an argument to the setInterval method. As another option, we can create an anonymous function and use it as a parameter for the setInterval method. Below is a code snippet that illustrates the utilization of anonymous functions with the setInterval method:

Example

setInterval(function() {
  console.log("Hello World!");
}, 5000);

Output:

Output

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

Explanation:

In the following code example, we create an unnamed function and pass it as an argument to the setInterval function. This unnamed function is designed to output the text "Hello World!" to the console. The setInterval method will repeatedly invoke the unnamed function at intervals of 5 seconds until it is manually terminated.

Using Arrow Functions

In ES6, a novel feature known as arrow functions was introduced. Arrow functions offer a streamlined approach to defining functions in JavaScript. Below is an example code snippet that illustrates the usage of arrow functions in conjunction with the setInterval method:

Example

setInterval(() => console.log("Hello World!"), 5000);

Output:

Output

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

Explanation:

In the following code example, we have employed an arrow function to create a function that is executed at regular intervals. This arrow function is designed to output the message "Hello World!" to the console. The setInterval method will invoke the arrow function every 5 seconds and will keep doing so until it is manually terminated.

Passing Arguments to the Function

In this illustration, we will provide parameters to the function that is intended to be executed multiple times. We can supply these parameters by incorporating them within the function invocation inside the setInterval method.

Example

function greet(name) {
  console.log(`Hello ${name}!`);
}
setInterval(greet, 5000, "John");

Output:

Output

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

Explanation:

In the following code example, we have established a function called greet that accepts a parameter named name and outputs a greeting message to the console. Subsequently, we utilize the setInterval method to invoke the greet function every 5 seconds, providing "John" as the argument for the name parameter.

Conclusion

In this article, we explored the process of invoking a function at regular intervals of 5 seconds in JavaScript. We discovered that the setInterval function can be utilized to repeatedly run a function at designated time intervals. The setInterval function serves as a robust mechanism in JavaScript, enabling developers to run code periodically at specified times. Furthermore, we examined the procedure for halting the execution of the setInterval function by employing the clearInterval function. Additionally, we covered the use of anonymous functions and arrow functions in conjunction with the setInterval function to create functions in a more succinct and graceful manner.

Input Required

This code uses input(). Please provide values below: