Methods for using SetInterval and settimeout in jquery _jquery

Source: Internet
Author: User
Tags setinterval time interval

Method 1. Application of jquery extension can solve this problem.

Copy Code code as follows:

$ (document). Ready (function () {
$.extend ({
Show:function () {
Alert ("Ready");
}
});
SetInterval ("Show ()", 3000);
});


Method 2. Do not use quotes and parentheses when specifying functions that are scheduled to execute.

Copy Code code as follows:

$ (function () {
Function Show () {
Alert ("Ready");
}
SetInterval (show,3000);/Note the function name has no quotes and parentheses!
Use SetInterval ("Show ()", 3000); Report "Missing objects"
});

Difference:

SetTimeout ()

To execute an expression or function by delaying the specified time from loading;
Execute once only; use with Window.cleartimeout.

SetInterval ()

At execution time, it executes an expression or function at a specified time after the page is loaded; (functions are similar to recursive functions); use with Window.clearinterval.

Supplementary notes:

Both of these methods can be used to implement JavaScript after a fixed time period. But both have their own application scenarios.

Method

In fact, settimeout and setinterval have the same syntax. They all have two parameters, one is the code string that will be executed, and one is the time interval in milliseconds, and the code will be executed after that time period.

However, these two functions are different, setinterval after the execution of the code, after the fixed time interval, it will also automatically repeat the code, and settimeout only one time to execute the code.

Although it appears that settimeout can only be applied to on-off actions, you can repeat the call to settimeout by creating a function loop:

Copy Code code as follows:

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 5 seconds. If you use SetInterval, the corresponding code looks like this:

Copy Code code as follows:

SetInterval ("ShowTime ()", 5000);
function ShowTime ()
{
var today = new Date ();
Alert ("The time is:" + today.tostring ());
}

The two methods may look very much alike, and the results shown will be similar, but the big difference is that the SetTimeout method does not perform a showtime function every 5 seconds, It executes the Showtime function 5 seconds after each call to the settimeout. This means that if the body part of the Showtime function takes 2 seconds to complete, the entire function is executed once every 7 seconds. But setinterval is not bound by the function it calls, it simply repeats the function once at a certain time.

If an action is required to be performed precisely after every fixed interval of time, it is best to use setinterval, and if you do not want to cause a problem of mutual interference due to successive calls, especially if the invocation of each function requires heavy computation and lengthy processing time, Then it's best to use settimeout.

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.