Home  >  Article  >  Web Front-end  >  javascript setinterval usage

javascript setinterval usage

WBOY
WBOYOriginal
2023-05-15 20:16:051220browse

JavaScript is one of the most commonly used front-end programming languages. It has powerful functions and flexible features. One of the important features is setInterval(). setInterval() is a JavaScript method used to create intervals for executing code. It allows you to call code repeatedly at specified intervals. In this article, we will introduce the usage of setInterval() in detail and provide some practical examples.

The basic syntax of the setInterval() method is as follows:

setInterval(function, milliseconds);

The first parameter is the code block to be executed, and the second The parameter is the interval between executing the code, in milliseconds.

The following is a simple example:

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

This code will output "Hello World!" every 1 second. The setInterval() method requires a function as parameter to execute the code block. If you need to pass parameters, you can use anonymous functions to achieve this. For example:

var name = "Mike";

setInterval(function(){
  console.log("Hello " + name + "!");
}, 1000);

This example will output "Hello Mike!" every 1 second. In this example, we used a variable to pass the name parameter for use in the code block.

The setInterval() method returns an identifier that can be used to clear the timer. The following is an example:

var timer = setInterval(function(){
  console.log("Hello World!");
}, 1000);

// 清除定时器
clearInterval(timer);

In this example, we first store the return value of the setInterval() method in the variable timer, and then use the clearInterval() method to clear the timer. This will stop the execution of the code block and clear the associated memory.

The setInterval() method can also be nested to execute code blocks on multiple time intervals. For example:

setInterval(function(){
  console.log("First block of code");
  setTimeout(function(){
    console.log("Second block of code");
  }, 500);
}, 1000);

In this example, we have defined two code blocks. The first block of code will output "First block of code" every 1 second. At the end of the first code block, we use the setTimeout() method to define the second code block, which will output "Second block of code" every 0.5 seconds. The two code blocks will have different time intervals.

Finally, let’s look at a practical example of using the setInterval() method. Let's say we are creating an online stopwatch. We need a timer to record the seconds and display the number on the screen. Here is the code for the app:




    Online Timer

Online Timer

0

The code includes three buttons: Start, Stop and Reset. When clicked on the start button, the startTimer() function is called, which creates a setInterval() function that executes every second. This function will record the elapsed time and display it on the screen. When the stop button is clicked, the stopTimer() function is called, which clears the call to the setInterval() function and stops the timer. When clicked on the reset button, the resetTimer() function is called, which resets the counter to 0 and then stops the timer.

In short, setInterval() is a very useful method in JavaScript, which allows you to easily execute blocks of code at specified intervals. Using it can greatly improve your JavaScript programming efficiency.

The above is the detailed content of javascript setinterval usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn