简体   繁体   English

重置JavaScript计数器?

[英]Reset JavaScript Counter?

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 我正在尝试通过JavaScript将计数器编程到网站中,并在这里找到我真正需要的: http : //www.w3schools.com/js/js_timing.asp

What I am trying to do is count down from 30 to 0. Once the counter reaches zero I want to display an alert. 我正在尝试从30倒数到0。一旦计数器达到零,我想显示一个警报。 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. 我无法弄清楚的唯一功能是如何在按下某个按钮时将计数器重置为30。 I have tried code such as document.gameForm.txt.value=30; 我已经尝试过诸如document.gameForm.txt.value=30;代码document.gameForm.txt.value=30; but for whatever reason 30 does not replace whatever text is currently in the box. 但是无论出于何种原因30都不会替换框中当前显示的任何文本。

As far as I understand it, you need to call clearTimeout() function to reset the counter eg: 据我了解,您需要调用clearTimeout()函数来重置计数器,例如:

var t = setTimeout("alert('I am displayed after 3 seconds!')", 3000);

Later you can reset it like: 稍后,您可以将其重置为:

clearTimeout(t);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM