Setinterval and setTimeout are used in jquery.

Source: Internet
Author: User

If you call other methods directly in ready, an error indicating that the object is missing is prompted. The solution is as follows:

Method 1. Apply jquery extension to solve this problem.

$ (Document). Ready (function (){

$. Extend ({
Show: function (){
Alert ("ready ");
}
});
Setinterval ("show ()", 3000 );
});

Method 2: Do not use quotation marks or parentheses when specifying the function to be executed regularly.

$ (Function (){
Function show (){
Alert ("ready ");
}
Setinterval (show, 3000); // note that the function name is not enclosed in quotation marks or arc!

// When setinterval ("show ()", 3000) is used, "an object is missing" is reported"

});

Differences:

SetTimeout ()

Execute an expression or function at the specified time after loading;

Run only once; Used with window. cleartimeout.

Setinterval ()

During execution, it starts from after loading the pageExecute at specified intervalsAn expression or function; (the function is similar to a recursive function); used with window. clearinterval.

Note:

Both methods can be used to execute JavaScript after a fixed period of time. However, they have different application scenarios.

Method

In fact, setTimeout and setinterval have the same syntax. Both of them have two parameters. One is to be executed.CodeString, and the interval in milliseconds. After that time period, the code will be executed.

However, there are differences between the two functions. After the setinterval code is executed, it will automatically re-execute the code at that fixed interval, setTimeout only executes the code once.

Although it seems that setTimeout can only be applied to On-Off actions, you can call setTimeout repeatedly by creating a function loop to implement repeated operations:

Showtime ();

Function Showtime ()

{

VaR today = new date ();

Alert ("the time is:" + today. tostring ());

SetTimeout ("Showtime ()", 5000 );

}

Once this function is called, the time is displayed every five seconds. If setinterval is used, the corresponding code is as follows:

Setinterval ("Showtime ()", 5000 );

Function Showtime ()

{

VaR today = new date ();

Alert ("the time is:" + today. tostring ());

}

The two methods may look very similar and show similar results, but the biggest difference between the two is that the setTimeout method does not execute the Showtime function every five seconds, it executes the Showtime function five seconds after each setTimeout call. This means that if the main part of the Showtime function needs to be executed in 2 seconds, the whole function will be executed every 7 seconds. While setinterval is not bound by the function called by itself, it simply repeats the function at a certain time.

If you want to execute an action accurately after a fixed interval, you 'd better use setinterval. If you don't want to interfere with each other due to continuous calls, in particular, each function call requires heavy computing and a long processing time, so it is best to use setTimeout.

Original article: http://www.cnblogs.com/meihua/articles/1917999.html

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.