Home  >  Article  >  Web Front-end  >  How to use the setInterval method

How to use the setInterval method

不言
不言Original
2019-01-30 15:35:3142362browse

The setInterval method is to call a function or calculation expression at a specified time interval. Its usage syntax is such as "setInterval(myFunction, myTimeLapse;)", where the parameter myFunction is the name of the function to be executed.

How to use the setInterval method

The operating environment of this article: Windows 7 system, Dell G3 computer, javascript version 1.8.5.

setInterval() is a native JavaScript function that can call functions or calculate expressions at specified time intervals (in milliseconds). In this article, we will take a look at the specific usage of the setInterval method.

Let’s take a look at the basic syntax of the setInterval method

setInterval(myFunction,myTimeLapse);

where myFunction is the name of the function to be executed, and myTimeLapse refers to the function to be executed after the specified time interval. code.

Let's look at an example

For example, if we want to output a "Hello!" every two seconds, we can use the setInterval method

The code is as follows

function helloFun():void { 
trace(“Hello!”); 
} 
setInterval(helloFun,2000);

The function is first executed after 2000 milliseconds (i.e. 2 seconds) have passed, and then executed again after every 2000 milliseconds.

With the above code, you will see the Hello output displayed in the window every two seconds! , if you want to stop, you need to use the clearInterval() method.

Let’s look at the example

The code is as follows

function helloFun():void { 
trace(“Hello!”); 
} 

var myInterval:uint = setInterval(helloFun,2000); 
clearInterval(myInterval);

This will stop the output.

The above is a brief introduction to the usage of the setInterval method. In fact, setInterval can be applied in many places, so I won’t go into details here. For more information, you can pay attention to the relevant content on the PHP Chinese website. .

The above is the detailed content of How to use the setInterval method. 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
Previous article:How to use Date objectNext article:How to use Date object