【问题标题】:Jquery set timeoutjQuery设置超时
【发布时间】:2014-06-05 10:36:26
【问题描述】:

我想在下面的代码中添加timeout

var faderIndex = 0,
        faders = $('.fadey');

    function nextFade() {
        $(faders[faderIndex]).fadeOut(1000, function() {
            faderIndex++;
            if (faderIndex >= faders.length)
                faderIndex = 0;
            $(faders[faderIndex]).fadeIn(1000, nextFade);
        });
    }
    nextFade();

http://jsfiddle.net/gpQYW/501/

您对如何实现这一点有什么建议吗?

【问题讨论】:

  • 出于什么目的?您现在使用回调的实现是一种更好的模式。

标签: jquery timeout settimeout


【解决方案1】:
    timeset=setTimeout(function() {
//do something 
    }, 8000);

     timeset=setTimeout(function() {
    var faderIndex = 0,
            faders = $('.fadey');

        function nextFade() {
            $(faders[faderIndex]).fadeOut(1000, function() {
                faderIndex++;
                if (faderIndex >= faders.length)
                    faderIndex = 0;
                $(faders[faderIndex]).fadeIn(1000, nextFade);
            });

        }
        nextFade();

    }, 8000);

【讨论】:

    【解决方案2】:

    尝试使用javascript超时

    var counter = 0;
    tid = setTimeout(mycode, 3000);
                    function mycode() {
                        counter++;
                        if (counter > 500) {
                            clearTimeout(tid);
                        tid=null;
                        return;
                        }
    
            tid = setTimeout(mycode, 10);
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      相关资源
      最近更新 更多