JavaScript学习05 定时器

 

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

 

posted @   圣骑士wind  阅读(716)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2013-04-22 SQL基础:从表中检索数据
点击右上角即可分享
微信分享提示