简体   繁体   English

jQuery:无限循环和暂停

[英]Jquery: Infinite loop and pause

I have this code 我有这个代码

var timeout = 0;
 $('#container td').each(function(){
   var td = this;
   setTimeout(function() {
     var new_text = $(td).find(text).html();
     popup_text.html(new_text);
     popup.fadeIn('fast').delay(1000).fadeOut('slow');
   }, timeout);
   timeout += 1000 + 1000;
});

I get text from table cells and is displayed in the layer with a delay. 我从表格单元格获取文本,并延迟显示在图层中。 1 question: How do I make this code to run in an endless loop? 1个问题:如何使这段代码无限循环地运行? 2 question: How to do that when you hover the mouse over popop cycle temporarily stopped and then continue? 2问题:将鼠标悬停在popop周期暂时停止然后继续时,该怎么办? Thanks a lot! 非常感谢!

One way is to put the code to be repeated in a function, and have the function repeat itself at the end: 一种方法是将要重复的代码放入函数中,并使函数在末尾重复自身:

var timeout = 1000;
var action = function() {
    // Do stuff here
    setTimeout(action, timeout);
};
action();

However, as ahren suggested, setInterval might be better: 但是,正如ahren所建议的那样,setInterval可能更好:

var timeout = 1000;
var action = function() {
    // Do stuff here
};
setInterval(action, timeout);

The difference is slight, but if the machine is running slowly for some reason, the setInterval version will run the code every second on average, whereas the setTimeout version will run the code once each second at most. 差别很小,但是如果计算机由于某种原因运行缓慢,则setInterval版本将平均每秒运行一次代码,而setTimeout版本将最多每秒运行一次代码。

Neither of those methods really work well with each(), however, so you'll need to store the sequence of popups somewhere and step through them: 但是,这两种方法都不能很好地与each()一起使用,因此您需要将弹出窗口的序列存储在某个位置并逐步执行它们:

var timeout = 1000;
var tds = $('#container td');
var index = 0;
var action = function() {
    var td = tds[index];
    var new_text = $(td).html();
    popup.html(new_text);
    popup.fadeIn('fast').delay(1000).fadeOut('slow');

    if(++index >= tds.length)
        index = 0;        
};
setInterval(action, timeout);
action();

Finally, to avoid moving to the next popup while the popup is hovered, you can add a check for that at the start of the function. 最后,为避免在悬停弹出窗口时移至下一个弹出窗口,可以在函数开始时添加对它的检查。 It's also necessary to rearrange the animations so that they go "check for hover - fade out - change text - fade in". 还必须重新排列动画,以便它们“检查悬停-淡出-更改文本-淡入”。

var timeout = 1000;
var tds = $('#container td');
var index = 0;
var action = function() {
    if(popup.is(':hover'))
        return;

    var td = tds[index];
    var new_text = $(td).html();
    popup.fadeOut('slow', function() {
        popup.html(new_text);
    }).fadeIn('fast');

    if(++index >= tds.length)
        index = 0;        
};
setInterval(action, timeout);
action();

jsFiddle: http://jsfiddle.net/qWkYE/2/ jsFiddle: http : //jsfiddle.net/qWkYE/2/

If you like the short clean way, then use the jquery-timing plugin and write: 如果您喜欢简洁的方法,请使用jquery-timing插件并编写:

$.fn.waitNoHover = function(){
  return this.is(':hover') ? this.wait('mouseleave') : this;
};
$('#popups div').repeat().each($).fadeIn('fast',$)
  .wait(200).waitNoHover().fadeOut('slow',$).all()

See this on http://jsfiddle.net/creativecouple/fPQdU/3/ 请参阅http://jsfiddle.net/creativecouple/fPQdU/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM