我试图在计时器上循环一个列表(目前是1秒,但我希望它更快)。问题是当前的值没有在视觉上被更新&我不明白为什么。
当我循环使用 firebug 时,按预期工作,但是没有firebug,它没有显示文本change...is,它以某种方式跳过了文本更新吗?
我将计时器设置为1秒;当然,.html()调用不会花费更长时间。谢谢你提供任何信息。
我的HTML很简单:
<div id="winner2"><h1></h1></div>
<div id="button"">
<img id="start" src="start.png" />
<img id="stop" src="stop.png" />
</div>
我的JS是:
var people = ["a", "b", "c"];
var counter = 1;
var run;
$(document).ready(function(){
$("#winner2 h1").html("a");
$("#stop").hide();
$( "#start" ).click(function() {
$("#start").hide();
$("#stop").show();
run = setTimeout (nameIncrement(),1000);
});
$( "#stop" ).click(function() {
$("#stop").hide();
$("#start").show();
clearTimeout(run);
});
function nameIncrement() {
if(counter == people.length) {
counter=0;
}
$("#winner2 h1").html(people[counter]);
counter++;
run = setTimeout (nameIncrement(),1000);
}
});
发布于 2014-02-07 23:17:52
您正在调用nameIncrement()
并将其返回值传递给setTimeout()
,而不是传递nameIncrement
。
删除括号:
run = setTimeout(nameIncrement, 1000);
发布于 2014-02-07 23:02:32
看起来,您正在运行一个递归函数,而不需要定义退出条件。这可能会导致浏览器超载,浏览器决定不运行该函数。
尝试:
function nameIncrement() {
if(counter == people.length) {
counter=0;
return;
}
$("#winner2 h1").html(people[counter]);
counter++;
run = setTimeout (nameIncrement(),1000);
}
});
然而,在调试模式下,浏览器不那么防御性强,所以您可以自己看到错误。
https://stackoverflow.com/questions/21639252
复制相似问题