javascript函数重新启动后,如何显示消息? [英] How do you display a message once a javascript function restarts?
问题描述
我想知道每次计时器重新开始时如何发送消息.到目前为止,这是我的代码:
Im wanting to know how to put a message in everytime the timer starts over. And here is my code thus far:
<head>
<script type="text/javascript">
var c=10;
var t;
var timer_is_on=0;
function timedCount() {
document.getElementById('txt').value = c;
c = c - 1;
if (c == 0)
c = 10;
}
function doMining() {
if (!timer_is_on) {
timer_is_on = true;
t = setInterval(function () {
timedCount();
}, 1000);
}
}
</script>
<SPAN STYLE="float:left">
<form>
<input type="button" value="Mining" onClick="doMining()">
<input type="text" id="txt">
</form>
</SPAN>
推荐答案
2个简单步骤:
- 为您的消息创建一个显示位置(即另一个网络元素)
- 在有条件的情况下,当计数器达到0时,更新消息元素的值
这是一个例子:
<div id='message'></div>
然后,访问该元素并使用DOM遍历(最好使用 dojo 或 jquery ,但您也可以手动执行):
Then, access that element and append your message or modify your method using DOM traversal (preferably using a javascript framework such as dojo or jquery but you can also do it manually):
if (c == 0) {
var _message = document.createTextNode("Timer has finished!");
document.getElementById('message').appendChild(_message);
c = 10;
}
此外,请勿在表单周围放置 SPAN .尝试使用"div"代替. Span用于对嵌入式文档元素进行样式设置.
Also, don't put a SPAN around a form. Try a "div" instead. Span's are meant for styling in-line document elements.
编辑:我假设当您说重新开始"时,您的意思是c = 0或计时器运行了10次.当它重新开始"时,也可能意味着计时器会重新调用该方法(例如,每1秒钟,在这种情况下,您只需将更新代码放在函数顶部)即可.
I'm assuming when you say "start over" you mean when the c = 0 or the timer has run 10 times. When it "starts over" could also mean when the method is re-called by the timer (i.e. every 1 second, in which case you'd just put the update code at the top of the function)
这篇关于javascript函数重新启动后,如何显示消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!