Use JavaScript Functions to set the delay for jquery execution

Source: Internet
Author: User

Set latency in JS:
Setinterval is similar to setTimeout.
SetTimeout is used to perform an operation after a delay period.
SetTimeout ("function", time) sets a timeout object

  1. Setinterval ("function", time) // sets a timeout object

Setinterval indicates automatic repetition, and setTimeout does not.
Cleartimeout (object) clears the set setTimeout object
Clearinterval (object) clears the set setinterval object
1. What is javas?Timer events
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. javasTimer functions

  1. SetTimeout () //-run the code after the specified time
  2. 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. getelementbyid (" + 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:

  1. Function delayrun (Code, time ){
  2. VaR T = setTimeout (Code, time );
  3. }

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.