admin管理员组文章数量:1024592
I am clearly missing something fundamental when it es to jQuery, anonymous functions, and delays.
The following code works only ONCE per page load (it will add the class, then remove it after 1 second, and if i click again, it will add the class, but will NEVER remove the class for the duration of the page, unless I reload the page):
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(){
$(this).removeClass("highlight");
});
HOWEVER,
if I add the (non-existant) function call as a parameter, AND I call it in my anonymous function, then the add/remove class bination will work indefinitely.
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(randomFunction){
$(this).removeClass("highlight");
randomFunction(); //this makes it seemingly 'miraculously' work??
});
Side Note:
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(randomFunction){
$(this).removeClass("highlight");
// this does NOT work; if I dont actually call the 'randomFunction'
// so that function, even though it does nothing; must somehow cause
// the implicit call of 'dequeue()' ??
});
I am clearly missing something fundamental when it es to jQuery, anonymous functions, and delays.
The following code works only ONCE per page load (it will add the class, then remove it after 1 second, and if i click again, it will add the class, but will NEVER remove the class for the duration of the page, unless I reload the page):
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(){
$(this).removeClass("highlight");
});
HOWEVER,
if I add the (non-existant) function call as a parameter, AND I call it in my anonymous function, then the add/remove class bination will work indefinitely.
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(randomFunction){
$(this).removeClass("highlight");
randomFunction(); //this makes it seemingly 'miraculously' work??
});
Side Note:
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(randomFunction){
$(this).removeClass("highlight");
// this does NOT work; if I dont actually call the 'randomFunction'
// so that function, even though it does nothing; must somehow cause
// the implicit call of 'dequeue()' ??
});
Share
Improve this question
edited Feb 14, 2013 at 21:15
Stevers
asked Feb 14, 2013 at 20:46
SteversStevers
5355 silver badges11 bronze badges
2
- Please tell us what exactly you are trying to achieve with the above code. – Venemo Commented Feb 14, 2013 at 20:48
- I was trying to mimic a "temporary highlight" for any input field that did not contain valid information. [For a form validation script]. I noticed thought that my exact code works, regardless of whether the function I pass in is named 'next' or not. Can someone explain this behavior? Its not enough to just pass in a function, you must also call it inside the anonymous function. I could see how function overloading may cause this implicit 'dequeue()' behavior, but the trigger itself exists INSIDE the anonymous function. – Stevers Commented Feb 14, 2013 at 21:11
2 Answers
Reset to default 5There is no miracle there. This behavior it's written in the documentation of .queue()
.
Note that when adding a function with
.queue()
, we should ensure that.dequeue()
is eventually called so that the next function in line executes.$('#foo').slideUp().queue(function() { alert('Animation plete.'); $(this).dequeue(); });
As of jQuery 1.4, the function that's called is passed another function as the first argument. When called, this automatically dequeues the next item and keeps the queue moving. We use it as follows:
$("#test").queue(function(next) { // Do some stuff... next(); });
the randomFunction
is actually referred to as next
and references the .dequeue
method. Calling it causes the queue to continue on to the next item in the queue.
http://api.jquery./queue/
I am clearly missing something fundamental when it es to jQuery, anonymous functions, and delays.
The following code works only ONCE per page load (it will add the class, then remove it after 1 second, and if i click again, it will add the class, but will NEVER remove the class for the duration of the page, unless I reload the page):
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(){
$(this).removeClass("highlight");
});
HOWEVER,
if I add the (non-existant) function call as a parameter, AND I call it in my anonymous function, then the add/remove class bination will work indefinitely.
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(randomFunction){
$(this).removeClass("highlight");
randomFunction(); //this makes it seemingly 'miraculously' work??
});
Side Note:
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(randomFunction){
$(this).removeClass("highlight");
// this does NOT work; if I dont actually call the 'randomFunction'
// so that function, even though it does nothing; must somehow cause
// the implicit call of 'dequeue()' ??
});
I am clearly missing something fundamental when it es to jQuery, anonymous functions, and delays.
The following code works only ONCE per page load (it will add the class, then remove it after 1 second, and if i click again, it will add the class, but will NEVER remove the class for the duration of the page, unless I reload the page):
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(){
$(this).removeClass("highlight");
});
HOWEVER,
if I add the (non-existant) function call as a parameter, AND I call it in my anonymous function, then the add/remove class bination will work indefinitely.
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(randomFunction){
$(this).removeClass("highlight");
randomFunction(); //this makes it seemingly 'miraculously' work??
});
Side Note:
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(randomFunction){
$(this).removeClass("highlight");
// this does NOT work; if I dont actually call the 'randomFunction'
// so that function, even though it does nothing; must somehow cause
// the implicit call of 'dequeue()' ??
});
Share
Improve this question
edited Feb 14, 2013 at 21:15
Stevers
asked Feb 14, 2013 at 20:46
SteversStevers
5355 silver badges11 bronze badges
2
- Please tell us what exactly you are trying to achieve with the above code. – Venemo Commented Feb 14, 2013 at 20:48
- I was trying to mimic a "temporary highlight" for any input field that did not contain valid information. [For a form validation script]. I noticed thought that my exact code works, regardless of whether the function I pass in is named 'next' or not. Can someone explain this behavior? Its not enough to just pass in a function, you must also call it inside the anonymous function. I could see how function overloading may cause this implicit 'dequeue()' behavior, but the trigger itself exists INSIDE the anonymous function. – Stevers Commented Feb 14, 2013 at 21:11
2 Answers
Reset to default 5There is no miracle there. This behavior it's written in the documentation of .queue()
.
Note that when adding a function with
.queue()
, we should ensure that.dequeue()
is eventually called so that the next function in line executes.$('#foo').slideUp().queue(function() { alert('Animation plete.'); $(this).dequeue(); });
As of jQuery 1.4, the function that's called is passed another function as the first argument. When called, this automatically dequeues the next item and keeps the queue moving. We use it as follows:
$("#test").queue(function(next) { // Do some stuff... next(); });
the randomFunction
is actually referred to as next
and references the .dequeue
method. Calling it causes the queue to continue on to the next item in the queue.
http://api.jquery./queue/
本文标签: javascriptjQuery toggle class with delay works only onceStack Overflow
版权声明:本文标题:javascript - jQuery toggle class with delay works only once - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745609351a2158911.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论