嘿伙计们,我正在建立一个简单的横幅旋转器.事实是,当它旋转而没有按下任何按钮时,工作正常,但当我按下某个按钮来更改横幅并清除时间时,它不起作用.
看起来时间不清楚.
谢谢!
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");
}
Run Code Online (Sandbox Code Playgroud)
Tra*_*ebb 15
因为你使用clearTimeout()不正确.您的代码需要类似于以下内容:
var x = setTimeout("doStuff();", tempo);
clearTimeout(x);
Run Code Online (Sandbox Code Playgroud)
您当前正在使用tempo超时句柄,这就是它无法正常工作的原因.
| 归档时间: |
|
| 查看次数: |
37339 次 |
| 最近记录: |