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