SetInterval and setTimeout usage in Javascript

Source: Internet
Author: User
JavaScript can be used to implement code execution in a delayed manner. That is to say, when a function is called, it does not execute some code immediately. Instead, it is executed after a specified period of time. This is called a timing event.

I. What is a JavaScript timing event?

Using JavaScript can implement code execution in a delayed manner. That is to say, when a function is called, it does not execute some code immediately, but waits for a specified period of time before execution. This is called a timing event.

Ii. Functions of JavaScript timing events

SetTimeout () //-run the code after the specified time

ClearTimeout () //-cancel setTimeout ()

Note: setTimeout () and clearTimeout () are both functions of the Window object of html dom...

Iii. setTimeout

Var t = setTimeout ("javascript statement", time parameter)

Note: The unit of time parameter is millisecond.

Example: var t = setTimeout ("alert ('3 seconds! ') ", 3000)

If the js statement carries a variable, the variable must be connected with the + sign, for example:

Var t = setTimeout ("document. getelementbyidx_xx_x_x_x_x_x (" + menuid + "). style. display = 'none'", 3000)

Iv. clearTimeout

Syntax: clearTimeout (variable name of setTimeout)

Example: clearTimeout (t) // where t is the setTimeout variable set earlier

You can use clearTimeout to stop timing at any time.

5. Application Skills

We recommend that you set setTimeout as a function. For example:

Function delayRun (code, time ){

Var t = setTimeout (code, time );

}

In this way, when you need to delay the execution of a code segment, you only need to add this function before the code segment. For example:

Onmouseover = delayRun ("setTab (500)

SetTab is a custom function. If you do not want to delay the execution of setTab in the future, remove the delayRun code in the statement,

Change to onmouseover = setTab (0, 0.

This method avoids writing a piece of setTimeout code for every place that requires latency. You only need to call it directly, which is very convenient. It also saves the amount of code.

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.