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)}.