Precautions for using setInterval and setTimeout in jQuery _ jquery

Source: Internet
Author: User
Recently I encountered a problem. When using jquery to write a timer, there will always be an error indicating that fn does not exist. When I write a timer, I am always used to direct

SetInterval ("fn ()", 2000 );
Recently encountered a problem. When using jquery to write a timer, the error message indicating that fn does not exist always appears, as shown below:

$ (Function () {setInterval ("fn ()", 2000 );})
The solution is to remove the quotation marks and brackets and use the original method.

$ (Function () {setInterval (fn, 2000 );})
Another method is to write the jq extension, as shown below:

The Code is as follows:


$ (Function (){
$. Extend ({
Fn: function (){
Alert ("im fn! ");
}
});
SetInterval ("$. fn ()", 2000 );
});


There is no problem with writing the above statements. But what if you want to pass the parameter?

Like the first method above,

$ (Function () {setInterval (fn, 2000 );})
If you write

$ (Function () {setInterval (fn (para), 2000 );})
An error is reported. This is classic and idiotic.

In this case, you can build a function and write it

$ (Function () {setInterval (function () {fn (para) }, 2000 );})
This is also possible.

As for how to transfer the second method, this is simpler, so I will not talk about it.

Posting to a blog is only for memory use. It is a foundation! It is also easy for beginners to make mistakes!


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

Or add the second method for passing parameters.

First look at the code segment

The Code is as follows:


$ (Function (){
$ Start = 1;
$. Extend ({
A: function (t ){
$ Index = t;
Alert ($ index );
$ Start ++;
}
});
SetInterval ("$. a (" + $ start + ")", 2000 );
});


Some people will try to write it like this. What is the result? The result is alert, which is always 1 and does not increase. Note that the first parameter in setInterval is a statement that is enclosed in double quotation marks and is interpreted as a variable. If you follow the preceding statement, it is equivalent

SetInterval ("$. a (1)", 2000 );
The result is understandable. Of course, this is the correct method.

The Code is as follows:


$ (Function (){
$ Start = 1;
$. Extend ({
A: function (t ){
$ Index = t;
Alert ($ index );
$ Start ++;
}
});
SetInterval ("$. a ($ start)", 2000 );
});


$ Start is interpreted as a variable. The statement is equivalent to function () {a (variable)} Instead of function () {a (value )}.
Related Article

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.