码迷,mamicode.com
首页 > 编程语言 > 详细

BOM学习-javascript计时器小结

时间:2017-10-14 22:36:29      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:技术分享   alt   body   自动   images   button   har   总结   eth   

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>自动增长计时器</title>
<script type="text/javascript">
  var atime;
  function clock(){
    var time=new Date();          
    atime=time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
    document.getElementById("clock").value = atime;
  }
 setInterval(clock,1000); 
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50"  style="background:#000;color:#00ff00;width:55px"; />
</form>
</body>
</html>

效果:

技术分享

显示当前时间,自动增长

 

添加按钮的计时器(用于开始和暂停)

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</title>

<script type="text/javascript">
  var num=0;
  var i;
  function startCount(){
    document.getElementById(count).value=num;
    num=num+1;
    i=setTimeout("startCount()",1000);
  }
  function stopCount(){
  clearTimeout(i);
  }
</script>
</head>
<body>
  <form>
    <input type="text" id="count" />
    <input type="button" value="Start" onclick="startCount()"/>
    <input type="button" value="Stop"  onclick="stopCount()" />
  </form>
</body>
</html>

效果:

技术分享

点击开始按钮,从0计数,一秒加1。点击按钮停止,保持当前状态。

 

总结:

1、setInterval(代码,交互时间);

参数:代码:可以是函数名或代码串;交互时间:设置交互时间

clearInterval(id_from_setInterval);

参数:由setInterval() 返回的 ID 值。

2、setTimeout(代码,延迟时间);

参数:代码: 可以是函数名或代码串;延迟时间:设置延迟时间

clearTimeout(id_from_setTimeout);

参数:由setTimeout()返回的ID值。

BOM学习-javascript计时器小结

标签:技术分享   alt   body   自动   images   button   har   总结   eth   

原文地址:http://www.cnblogs.com/zmx-xiao-xiao/p/7668760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!