javascript函数重新启动后,如何显示消息?

编程入门 行业动态 更新时间:2024-10-26 12:27:48
本文介绍了javascript函数重新启动后,如何显示消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道每次计时器重新开始时如何发送消息.到目前为止,这是我的代码:

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函数重新启动后,如何显示消息?

    本文发布于:2023-11-25 12:23:51,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1629737.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:重新启动   函数   消息   javascript

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!