重置JavaScript计数器? [英] Reset JavaScript Counter?
问题描述
我正在尝试通过JavaScript将计数器编入网站,并在此处找到了我需要的内容:
http://www.w3schools.com/js/js_timing.asp
I am trying to program a counter into a website via JavaScript and have found exactly what I need here: http://www.w3schools.com/js/js_timing.asp
我要做的是倒数从30一旦计数器达到零,我想显示一个警告。我能够让这个工作。我唯一无法弄清楚的功能是如何在某个按钮按下时将计数器重置回30。我已经尝试了诸如 document.gameForm.txt.value = 30;
之类的代码,但无论出于何种原因,30都不会替换当前框中的任何文本。
What I am trying to do is count down from 30 to 0. Once the counter reaches zero I want to display an alert. I have been able to get this to work. The only function that I cannot figure out is how to reset my counter back to 30 on a certain button push. I have tried code such as document.gameForm.txt.value=30;
but for whatever reason 30 does not replace whatever text is currently in the box.
推荐答案
据我了解,你需要打电话给 clearTimeout()
重置计数器的功能例如:
As far as I understand it, you need to call clearTimeout()
function to reset the counter eg:
var t = setTimeout("alert('I am displayed after 3 seconds!')", 3000);
稍后您可以将其重置为:
Later you can reset it like:
clearTimeout(t);
这篇关于重置JavaScript计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!