Jquery .delay().fadeOut cancel/clear queue.. Is it possible? How?

房东的猫 提交于 2019-12-24 00:38:23

问题


I need some help here.. Is it possible to cancel the chaining delay?

Mn.Base.TopBox.show = function(timedur){
    $('#element').fadeIn().delay(timedur).fadeOut();
}


Mn.Base.TopBox.cancelFadeout = function(){

}

I read about queuing and tried some different approaches but I hadn't success...

    $('#element').stop();

    $('#element').queue('fx', []);

Thanks in advance,

Pedro


回答1:


It isn't, .delay() doesn't play well with anything else since the timer keeps ticking and a .dequeue() is executed when it's up...regardless of if you cleared the queue and added a whole new one.

It's better to use setTimeout() directly if you intend to cancel, for example:

Mn.Base.TopBox.show = function(timedur){
  $('#element').fadeIn(function() {
    var elem = $(this);
    $.data(this, 'timer', setTimeout(function() { elem.fadeOut(); }, timedur));
  });
}

Mn.Base.TopBox.cancelFadeout = function(){
  clearTimeout($('#element').stop().data('timer'));
}

What this does is set the timer and store it using $.data(), and when clering the animations, we're both calling .stop() to stop anything in process, and stopping that timer.

There's still the potential here for issues if you're firing this very rapidly, in which case you'd want to switch to storing an array of delays, and clear them all.



来源:https://stackoverflow.com/questions/3863955/jquery-delay-fadeout-cancel-clear-queue-is-it-possible-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!