clearTimeout不工作 [英] clearTimeout not working

查看:121
本文介绍了clearTimeout不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我正在建立一个简单的横幅旋转器。事实是,当它旋转而没有按下任何按钮时,工作正常,但是当我按下某个按钮来更改横幅并清除时间时,它不起作用。



看起来比如时间不清楚。



谢谢!

  var tempo = 5000; 
var elemento;
var quantos;
var perual;

// Inicia

$(凭证).ready(function(){
bannerRotator(#destaques);

});


//FunçõesdoBanner


function bannerRotator(element){

// Conta quantos banners existem:
$('< ul class =buttons>< / ul>')。appendTo(element);
i = 0;
$(element).find(。banner)。each(function(){
$(element).find(。banner)。eq(i).addClass(id + i);
buttons = element +ul.buttons;
acId = i + 1;
$('< li>< a href =javascript:getBanner('+ i +');>'+ acId +'< / a>< / li>')。appendTo(buttons);
i ++;
});

// Inicia a rotacao
elemento = element;
quantos = i;
rotate(i,-1);

}

函数getBanner(r){
r = r-1;
rotate(quantos,r);
}


函数rotate(i,base){

clearTimeout(tempo);

if(base< i-1){
base ++;
atual = base;
setTimeout('rotate('+ i +','+ base +');',tempo);
}
else {
base = 0;
atual = base;
setTimeout('rotate('+ i +','+ base +');',tempo);
}

// Faz os淡化

$(elemento).find(。banner)。animate({opacity:0,});
$(elemento).find(。banner)。eq(base).animate({opacity:1,});

// Arruma os botoes

$(elemento).find(ul.buttons li)。removeClass(active);
$(elemento).find(ul.buttons li)。eq(base).addClass(active);

}


解决方案

因为你'错误地使用 clearTimeout()。您的代码需要类似于以下内容:

  var x = setTimeout(doStuff();,tempo); 
clearTimeout(x);

您目前正在使用 tempo 作为超时处理,这就是为什么它不起作用。


Hey guys, im building a simple banner rotator. The fact is, when it rotates without any buton pressed, works fine, but when i press some button to change the banner and clear the time, its doesnt work.

Looks like the time does not clear.

Thanks!

        var tempo = 5000;
        var elemento;
        var quantos;
        var atual;

        // Inicia

        $(document).ready(function() {
            bannerRotator("#destaques");

        });


        // Funções do Banner


        function bannerRotator(element) {

            // Conta quantos banners existem:
            $('<ul class="buttons"></ul>').appendTo(element);
            i = 0;
            $(element).find(".banner").each(function() {
                $(element).find(".banner").eq(i).addClass("id"+i);
                buttons = element+" ul.buttons";
                acId = i+1;
                $('<li><a href="javascript:getBanner('+i+');">'+acId+'</a></li>').appendTo(buttons);
                i++;
            });

            // Inicia a rotacao
            elemento = element;
            quantos = i;
            rotate(i,-1);

        }

        function getBanner(r) {
            r = r-1;
            rotate(quantos, r);
        }


        function rotate(i, base) {

            clearTimeout(tempo);

            if (base<i-1) {
                base++;
                atual = base;
                setTimeout('rotate('+i+', '+base+');', tempo);
            }
            else {
                base = 0;
                atual = base;
                setTimeout('rotate('+i+', '+base+');', tempo);
            }

            // Faz os fades

            $(elemento).find(".banner").animate({opacity: 0,});
            $(elemento).find(".banner").eq(base).animate({opacity: 1,});

            // Arruma os botoes

            $(elemento).find("ul.buttons li").removeClass("active");
            $(elemento).find("ul.buttons li").eq(base).addClass("active");

        }

解决方案

Because you're using clearTimeout() incorrectly. Your code needs to resemble the following:

var x = setTimeout("doStuff();", tempo);
clearTimeout(x);

You are currently using tempo as the timeout handle, which is why it isn't working.

这篇关于clearTimeout不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆