Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this questionHow to put multiple countdown timers on one page, each countdown pointing out to a different period of time, and of course, not reseting on page-refresh?
I'm wondering开发者_如何转开发 if there exists a jQuery solution for that?
Thank you!
try this:
<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>
精彩评论