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

JavaScript学习05 定时器

时间:2015-10-23 21:26:26      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:

JavaScript学习05 定时器

 

定时器1

  用以指定在一段特定的时间后执行某段程序。

  setTimeout()

  格式:[定时器对象名=] setTimeout(“<表达式>”,毫秒)

  功能:执行<表达式>一次。

  例子:

技术分享
<!DOCTYPE html>
<html>
  <head>
    <title>timer1.html</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

    <script type="text/javascript">
     function count()
     {
         setTimeout("alert(‘执行成功!‘)",7000);
     }
    </script>
  </head>
  
  <body>
    <input type="button" value="点击我啊" onclick="count();">
  </body>
</html>
技术分享

 

 

定时器2

  以一定的时间为间隔,不断地重复执行表达式。

  setInterval()

  格式:[定时器对象名=] setInterval(“<表达式>”,毫秒)

  功能:重复执行<表达式>,直至窗口、框架被关闭或执行clearInterval。

  clearInterval()

  格式:clearInterval(定时器对象名)  

  功能:终止定时器

  例子:

技术分享
<!DOCTYPE html>
<html>
  <head>
    <title>timer2.html</title>
    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    <script type="text/javascript">

    var sec = 0;
    var timer = setInterval("count();",1000);//页面加载的时候即开始计时
     function count()
     {
        document.getElementById("num").innerHTML = sec++;
         
     }
     
     function stopCount()
     {
         clearInterval(timer);//停止定时器的运行
     }
    </script>
  </head>
  
  <body>
    <font color="red" id="num">0</font>
    <input type="button" value="停止" onclick="stopCount();">
  </body>
</html>
技术分享

 

参考资料

  圣思园张龙老师Java Web视频教程。

  W3School JavaScript教程:http://www.w3school.com.cn/js/index.asp

  英文版:http://www.w3schools.com/js/default.asp

 

  JS Timing:http://www.w3school.com.cn/js/js_timing.asp

JavaScript学习05 定时器

标签:

原文地址:http://www.cnblogs.com/liu-Gray/p/4819239.html

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