gpt4 book ai didi

javascript - 为什么带有毫秒参数的 clearTimeout 不起作用?

转载 作者:数据小太阳 更新时间:2023-10-29 04:58:36 26 4
gpt4 key购买 nike

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

看来时间还不清楚。

        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");

}

最佳答案

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

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

您当前正在使用 tempo 作为超时句柄,这就是它不起作用的原因。

关于javascript - 为什么带有毫秒参数的 clearTimeout 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5397117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com