暂停/恢复jQuery延迟()

时间:2017-12-20 00:04:25

标签: javascript jquery delay

假设我有一个匿名函数,它会淡入淡出并循环遍历字符串数组,如下所示:

function cycle() {
  $elem.eq(currentElem).addClass('fading-in').fadeIn(1000,function(){
    $elem.eq(currentElem)removeClass('.fading-in').delay(4000).queue(function(next){
      $(this).addClass('fading-out');
      next();
      $(this).fadeOut(1000, function(){
          $elem.eq(currentElem).removeClass('fading-out');
          $elem.eq(currentElem).dequeue();
          currentElem = ((currentElem + 1) % elemArrLen); 
          cycle();
          });   
        });
  });
};

并像这样被调用:

// Initiate cycle function

setTimeout(function() {
  cycle();
}, 1000);

点击按钮有没有办法暂停/恢复此功能?类似的东西:

$('button').click(function() {
//if hasClass clicked
  cycle(); // but resuming where the previous click left off
// else
  $elem.eq(currentElem).clearQueue();
  $elem.eq(currentElem).stop();
  $(this).addClass('clicked');
});

注意:我不想使用setTimeoutsetInterval,因为当浏览器标签处于非活动状态时,它们会出现奇怪的行为。

0 个答案:

没有答案
相关问题