最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery .delay().fadeOut cancelclear queue.. Is it possible? How? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Oct 5, 2010 at 13:14 Pedro GilPedro Gil 5412 gold badges7 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

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.

发布评论

评论列表(0)

  1. 暂无评论