zoukankan      html  css  js  c++  java
  • JavaScript Timing 事件及两种时钟写法

    JavaScript 可以在时间间隔内执行。

    这就是所谓的定时事件( Timing Events)。

    --------------------------------------------------------------------------------------------------------------------------------------------

    Timing 事件

    window 对象允许以指定的时间间隔执行代码。

    这些时间间隔称为定时事件。

    通过 JavaScript 使用的有两个关键的方法:

    setTimeout(functionmilliseconds)
    在等待指定的毫秒数后执行函数。
    setInterval(functionmilliseconds)
    等同于 setTimeout(),但持续重复执行该函数,可以用来写时钟,详情见下方例子

    setTimeout() 和 setInterval() 都属于 HTML DOM Window 对象的方法。

    setTimeout() 方法

    window.setTimeout(function, milliseconds);

    window.setTimeout() 方法可以不带 window 前缀来编写。

    第一个参数是要执行的函数。

    第二个参数指示执行之前的毫秒数。

    实例

    单击按钮。等待 3 秒,然后页面会提示 "Hello":

    <button onclick="setTimeout(myFunction, 3000)">试一试</button>
    
    <script>
    function myFunction() {
        alert('Hello');
     }
    </script>

    完整实例:

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>点击“试一试”。等待 3 秒钟,页面将提示“Hello”。</p>
    
    <button onclick="setTimeout(myFunction, 3000);">试一试</button>
    
    <script>
    function myFunction() {
      alert('Hello');
    }
    </script>
    
    </body>
    </html>
    

    --------------------------------------------------------------------------------------------------------------------------------------------

    如何停止执行?

    clearTimeout() 方法停止执行 setTimeout() 中规定的函数。

    window.clearTimeout(timeoutVariable)

    window.clearTimeout() 方法可以不带 window 前缀来写。

    clearTimeout() 使用从 setTimeout() 返回的变量:

    myVar = setTimeout(function, milliseconds);
    clearTimeout(myVar);
    

    实例

    类似上例,但是添加了“停止”按钮:

    <button onclick="myVar = setTimeout(myFunction, 3000)">试一试</button>
    
    <button onclick="clearTimeout(myVar)">停止执行</button>

    完整实例:

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>点击“试一试”。等 3 秒。该页面将提醒“Hello”。</p>
    
    <p>单击“停止”以阻止第一个函数执行。</p>
    
    <p>(在 3 秒钟之前,您必须单击“停止”。)</p>
    
    <button onclick="myVar = setTimeout(myFunction, 3000)">试一试</button>
    
    <button onclick="clearTimeout(myVar)">停止</button>
    
    <script>
    function myFunction() {
      alert("Hello");
    }
    </script>
    </body>
    </html>

    ---------------------------------------------------------------------------------------------

    setInterval() 方法

    setInterval() 方法在每个给定的时间间隔重复给定的函数。

    window.setInterval(function, milliseconds);

    window.setInterval() 方法可以不带 window 前缀来写。

    第一个参数是要执行的函数。

    第二个参数每个执行之间的时间间隔的长度。

    本例每秒执行一次函数 "myTimer"(就像数字手表)。

    实例

    显示当前时间:

    var myVar = setInterval(myTimer, 1000);
     
    function myTimer() {
        var d = new Date();
        document.getElementById("demo").innerHTML = d.toLocaleTimeString();
    }

    完整实例

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>此页面上的脚本启动这个时钟:</p>
    
    <p id="demo"></p>
    
    <script>
    var myVar = setInterval(myTimer, 1000);
    
    function myTimer() {
      var d = new Date();
      document.getElementById("demo").innerHTML = d.toLocaleTimeString();
    }
    </script>
    
    </body>
    </html>

    一秒有 1000 毫秒。

    --------------------------------------------------------------------------------------------------------------------------------------------

    如何停止执行?

    clearInterval() 方法停止 setInterval() 方法中指定的函数的执行。

    window.clearInterval(timerVariable)

    window.clearInterval() 方法可以不带 window 前缀来写。

    clearInterval() 方法使用从 setInterval() 返回的变量:

    myVar = setInterval(function, milliseconds);
    clearInterval(myVar);
    

    实例

    类似上例,但是我们添加了一个“停止时间”按钮:

    <p id="demo"></p>
    
    <button onclick="clearInterval(myVar)">停止时间</button>
    
    <script>
    var myVar = setInterval(myTimer, 1000);
     function myTimer() {
        var d = new Date();
        document.getElementById("demo").innerHTML = d.toLocaleTimeString();
    }
    </script>

    完整实例:

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>此页面上的脚本启动这个时钟:</p>
    
    <p id="demo"></p>
    
    <button onclick="clearInterval(myVar)">停止时间</button>
    
    <script>
    var myVar = setInterval(myTimer ,1000);
    function myTimer() {
      var d = new Date();
      document.getElementById("demo").innerHTML = d.toLocaleTimeString();
    }
    </script>
    
    </body>
    </html>
    

     

    更多实例

    另一个简单的计时
    <!DOCTYPE html>
    <html>
    <body>
    
    <button onclick="timedText()">试一试</button>
    
    <p id="demo">点击“试一试”。我将在两秒,四秒和六秒过后显示。</p>
    
    <script>
    function timedText() {
      setTimeout(myTimeout1, 2000) 
      setTimeout(myTimeout2, 4000) 
      setTimeout(myTimeout3, 6000) 
    }
    function myTimeout1() {
      document.getElementById("demo").innerHTML = "2 秒";
    }
    function myTimeout2() {
      document.getElementById("demo").innerHTML = "4 秒";
    }
    function myTimeout3() {
      document.getElementById("demo").innerHTML = "6 秒";
    }
    </script>
    
    </body>
    </html>
    由计时时间创建的时钟
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function startTime() {
      var today = new Date();
      var h = today.getHours();
      var m = today.getMinutes();
      var s = today.getSeconds();
      m = checkTime(m);
      s = checkTime(s);
      document.getElementById('txt').innerHTML =
      h + ":" + m + ":" + s;
      var t = setTimeout(startTime, 500);
    }
    function checkTime(i) {
      if (i < 10) {i = "0" + i};  // 在数字 < 10 之前加零
      return i;
    }
    </script>
    </head>
    
    <body onload="startTime()">
    
    <div id="txt"></div>
    
    </body>
    </html>
    

      

  • 相关阅读:
    WebService中Dataset的压缩序列化和解压反序列化(DataSetSurrogate的使用)
    IOleControl 代码一,测试有问题,备忘
    关于Stream和byte之间的转换 .
    webbrowser 实现IOleControl接口2
    JavaScropt获取网页、浏览器、屏幕高度和宽度
    Oracle默认的用户名和密码
    window.showModalDialog()用法及注意事项
    Android1.6开发环境配置
    string与stream互相转换
    GirdView实现单选、FooterTemplate实现新建和PagerTemplate实现分页
  • 原文地址:https://www.cnblogs.com/zmdComeOn/p/10979497.html
Copyright © 2011-2022 走看看