我正在尝试循环遍历计时器上的列表(当前为 1 秒,但我希望它更快)。问题是当前值没有在视觉上更新,我不明白为什么。
当我循环遍历时Firebug it works按预期但没有萤火虫它没有显示文本更改...它是否以某种方式跳过文本更新?
我把定时器设置为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);
}
});
你正在呼唤nameIncrement()
并将其返回值传递给setTimeout()
,而不是通过nameIncrement
.
去掉括号:
run = setTimeout(nameIncrement, 1000);
jsfiddle
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)