jsの Timer

Source: Internet
Author: User
Tags setinterval

JavaScript Timing Events

By using JavaScript, we have the ability to execute code after a set interval of time, not immediately after the function is called. We call this a timing event.

It is easy to use timing events in JAVASCRITP, and two key methods are:

    • SetInterval ()-interval specifies the number of milliseconds to execute the specified code continuously .
    • SetTimeout ()-executes the specified code after pausing the specified number of milliseconds
1.setInterval () method

SetInterval () interval specifies the number of milliseconds to execute the specified code repeatedly

Grammar
Window.setinterval ("JavaScript function", milliseconds);

The Window.setinterval () method can use the function setinterval ()directly without using the window prefix.

SetInterval () The first parameter is a function.

The number of milliseconds for the second parameter interval

PS: 1000 milliseconds is one second.

How do I stop execution?

The Clearinterval () method is used to stop the function code executed by the SetInterval () method.

Grammar
Window.clearinterval (intervalvariable)

The Window.clearinterval () method can use the function clearinterval ()directly without using the window prefix.

To use the Clearinterval () method, you must use a global variable when you create the timing method:

Myvar=setinterval ("JavaScript function",milliseconds);

You can then use the Clearinterval () method to stop execution.

2.setTimeout () method syntax
T=window.settimeout (JavaScript function ", number of milliseconds);

The SetTimeout () method returns a value. In the above statement, the value is stored in a variable named T. If you want to cancel the setTimeout (), you can use the variable name to specify it.

The first argument to SetTimeout () is a string containing a JavaScript statement. This statement may be such as "alert (' 5 seconds! ')" or a call to a function, such as alertmsg () ".

The second parameter indicates how many milliseconds from the current start to execute the first parameter.

How do I stop execution?

The Cleartimeout () method is used to stop executing the function code of the SetTimeout () method.

Grammar
Window.cleartimeout (timeoutvariable)

The window.cleartimeout () method can not use the window prefix.

To use the Cleartimeout () method, you must use a global variable in the Create Timeout method (SetTimeout):

Myvar=settimeout ("JavaScript function",milliseconds);

If the function has not yet been executed, you can use the Cleartimeout () method to stop executing the function code.

The following code is my previous click on the conversion picture code of the upgraded version, that is, every 3 seconds to automatically convert a picture

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"></Head><Bodyonload= "Gaigai ()"><Script>functionGaigai () {setinterval (function() {element=document.getElementById ('im')    if(Element.src.match ("2") ) {element.src="./1.jpg"; }    Else{element.src="./2.jpg"; }}, the);}</Script><imgID= "Im"src= "./2.jpg" /></Body></HTML>

jsの Timer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.