Considerations for the use of setinterval, settimeout in jquery _jquery

Source: Internet
Author: User
Tags extend setinterval
Used to write the timer, always used to direct

SetInterval ("FN ()", 2000);
Recently encountered a problem, when using jquery to write the timer, there will always be FN does not exist error prompts, as follows

$ (function () {setinterval ("FN ()", 2000);})
The workaround is to remove the quotes and parentheses and use the original method

$ (function () {setinterval (fn,2000);})
The other is the extension of the writing JQ, as follows
Copy Code code as follows:

$ (function () {
$.extend ({
Fn:function () {
Alert ("Im fn!");
}
});
SetInterval ("$.fn ()", 2000);
});

All the above writing is no problem. But what if you need to pass the parameters?

Like the first one,

$ (function () {setinterval (fn,2000);})
If you write

$ (function () {setinterval (FN (para), 2000);})
It's an error. This is more classic, more idiotic.

Then you can build a function and write

$ (function () {setinterval (function () {FN (para)},2000);})
This is also possible.

As for how the second method is passed, this is much simpler, and I will not say more.

Send to the blog only when memory use, is the basis Ah! is also easy for beginners to make mistakes in the place!


//========================

Or add the second method of reference.

Read the code first.
Copy Code code as follows:

$ (function () {
$start = 1;
$.extend ({
A:function (t) {
$index = t;
alert ($index);
$start + +;
}
});
SetInterval ("$.A + $start +") ", 2000);
});

Some people will try to write this, what is the result? The result is that alert, which has been 1, will not increase. Note Here is the first argument inside the SetInterval, which is a statement that is enclosed in double quotes and the contents are interpreted as variables. If you follow the above, the equivalent

SetInterval ("$.a (1)", 2000);
Then the result is understandable. The right way, of course.
Copy Code code as follows:

$ (function () {
$start = 1;
$.extend ({
A:function (t) {
$index = t;
alert ($index);
$start + +;
}
});
SetInterval ("$.a ($start)", 2000);
});

Then $start will be interpreted as a variable. Statement is equivalent to function () {A (variable)} instead of function () {A (value)}.

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.