Did you have a problem writing jquery recently?
I was surprised to find that the effect of the "one Day" button was not removed. jquery is out of the question?I did not hesitate to ask myself such a question, and then I vetoed it. Why? Because I think I think more ...
But it's a matter to understand.
But the question is below, why does the settimeout () below not work?
I can't think of the solution.
So, I printed something like this in the anonymous function of settimeout.
Cosole.log (This = = = window);
Return value True//////? What.
How can this, I was writing to play ...
So I took a closer look.
Originally
In the traditional OnEvent property code, this reference receives the event element---but only in the property, not in the function called by the slave.
What does that mean? It is indeed in our click. But if you call the closure function inside, the This object again points back to our window object.
How to solve this problem ... Well done.
<span style= "FONT-SIZE:18PX;" ><script type= "Text/javascript" >$ (function () {$ (' a '). Click (Function (event) {$this = $ (this); $this. addclass (' clicked '); SetTimeout (function () {Console.log (this = = window), $this. Removeclass (' clicked ');//2},3000);}); </script></span>
That's right. Save $ (this) as a local variable: So why is it possible to copy this or $ (this) to a local variable to solve this problem?JavaScript creates a closure for local variables of the parameter kernel function.
Closures can be summed up as a 4 of the content.
1 You can nest another function in a JavaScript function, and nesting can be multilevel.
The 2 function can not only read its own parameters and local variables, but also read and write the variables in the nested function.
3 even if an external function has returned, it is also valid to invoke the inner function. Like settimeout.
4 whether anonymous or named functions are the same. This is a special JavaScript keyword, so none of these principles apply, and by copying the value of this to a local variable, you can use the closure to make the value available in any nested function.
Best Wishes.