如何在一个页面上放置多个倒数计时器,每个倒计时指出一个不同的时间段,当然,不会重新设置页面刷新?
我想知道是否存在 jQuery 解决方案?
谢谢!
答案 0 :(得分:1)
试试这个:
<input type="text" id="but1" />
<input type="text" id="but2" />
<input type="text" id="but3" />
<input type="button" value="start" onclick="timer(1)" />
<input type="button" value="start" onclick="timer(2)" />
<input type="button" value="start" onclick="timer(3)" />
<input type="button" value="stop" onclick="stoptimer(1)" />
<input type="button" value="stop" onclick="stoptimer(2)" />
<input type="button" value="stop" onclick="stoptimer(3)" />
<script type="text/javascript">
var counts = [];
var xs = [];
for (var i = 1; i < 10; i++) {
counts.push(i*2);
xs.push(0);
};
function timer(id)
{
xs[id]=setTimeout("timer("+id+")",1000);
counts[id]=counts[id]-1;
document.getElementById("but"+id).value=counts[id];
if (counts[id] <1){counts[id]=5;}
}
function stoptimer(id)
{
clearTimeout(xs[id]);
}
</script>