Home  >  Article  >  Web Front-end  >  Detailed explanation of the use of javascript timer events_Basic knowledge

Detailed explanation of the use of javascript timer events_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:04:531074browse

Using timing events in JavaScript is easy, two key methods are:

setTimeout()
Execute code at some time in the future

clearTimeout()
Cancel setTimeout()
setTimeout()
Syntax

Copy code The code is as follows:

var t=setTimeout("javascript statement" , milliseconds)


The setTimeout() method will return a certain value. In the above statement, the value is stored in a variable named t. If you wish to cancel this setTimeout(), you can specify it using this variable name.
The first parameter of setTimeout() is a string containing JavaScript statements. This might be a statement such as "alert('5 seconds!')", or a call to a function such as alertMsg()".

The second parameter indicates how many milliseconds from now the first parameter will be executed.

Tip: 1000 milliseconds equals one second.

When the button in the example below is clicked, a prompt box will pop up after 5 seconds.

Copy code The code is as follows:










Example - Infinite Loop

To create a timer that runs in an infinite loop, we need to write a function that calls itself. In the example below, the input field starts counting from 0 when the button is clicked.

Copy code The code is as follows:











clearTimeout()

Syntax

Copy code The code is as follows:

clearTimeout(setTimeout_variable)

Example

The following example is similar to the infinite loop example above. The only difference is that now we've added a "Stop Count!" button to stop the counter:

Copy code The code is as follows:













Two other important methods:
Copy code The code is as follows:

setInterval()
setInterval() - executes a function, over and over again, at specified time intervals

Function Yes: Execute a method in a loop within a specified interval

Syntax:

Copy code The code is as follows:

window.setInterval("javascript function",milliseconds);

Note: The first parameter must be a function, and the second parameter is the interval between executing the function.

Example:

Copy code The code is as follows:




Note: In the above example, the execution effect is to alert("hello") every 500ms;

Another clock:

Copy code The code is as follows:






 

How to stop, setInterval() method??

Copy code The code is as follows:

window.clearInterval()

Syntax:
Copy code The code is as follows:

window.clearInterval(intervalVariable)


Copy code The code is as follows:

The window.clearInterval() method can be written without the window prefix.

To be able to use the clearInterval() method, you must use a global variable when creating the interval method:

myVar=setInterval("javascript function",milliseconds);
Then you will be able to stop the execution by calling the clearInterval() method.

Example:

Copy code The code is as follows:




stop




}



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