zoukankan      html  css  js  c++  java
  • JavaScript--定时器setTimeout()、clearTimeout(var param)和setInterval()、clearInterval(var param)

       1.setTimeout()、clearTimeout(var param)

    • setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式,只调用一次
    • clearTimeout() 方法可取消由 setTimeout() 方法设置的定时操作,参数必须是由 setTimeout() 返回的 ID 值
    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style>
                #mytime {
                    background: #bbb;
                    color: #fff;
                    display: block;
                }
                
                .wrapper {
                    text-align: center;
                    width: 60%;
                    margin: 250px auto;
                }
            </style>
        </head>
    
        <body>
    
            <div class="wrapper">
                <h1 id=mytime>显示时间</h1>
                <button id="stop" name="button" onclick="stop()">stop</button>
                <button id="start" name="button" onclick="start()">start</button>
                <button id="reset" name="button" onclick="reset()">reset</button>
            </div>
        </body>
        <script type="text/javascript">
            var h = m = s = ms = 0; //定义时,分,秒,毫秒并初始化为0;
            var time = 0;
    
            function timer() { //定义计时函数
                ms = ms + 50; //毫秒
                if(ms >= 1000) {
                    ms = 0;
                    s = s + 1; //
                }
                if(s >= 60) {
                    s = 0;
                    m = m + 1; //分钟
                }
                if(m >= 60) {
                    m = 0;
                    h = h + 1; //小时
                }
                str = toDub(h) + "" + toDub(m) + "" + toDub(s) + "" + toDubms(ms) + "毫秒";
                mytime = document.getElementById('mytime');
                mytime.innerHTML = str;
                
                time = setTimeout(timer, 50); 
            }
    
            function reset() { //重置
                clearInterval(time);
                h = m = s = ms = 0;
                document.getElementById('mytime').innerHTML = "00时00分00秒0000毫秒";
                document.getElementById("start").removeAttribute("disabled");
                document.getElementById("stop").setAttribute("disabled", true);
            }
    
            function start() { //开始
                time =setTimeout(timer,50);
                document.getElementById("start").setAttribute("disabled", true);
                document.getElementById("stop").removeAttribute("disabled");
            }
    
            function stop() { //暂停
                clearTimeout(time);
                document.getElementById("start").removeAttribute("disabled");
                document.getElementById("stop").setAttribute("disabled", true);            
            }
    
            function toDub(n) { //补0操作
                if(n < 10) {
                    return "0" + n;
                } else {
                    return "" + n;
                }
            }
    
            function toDubms(n) { //给毫秒补0操作
                if(n < 10) {
                    return "00" + n;
                } else {
                    return "0" + n;
                }
    
            }
        </script>
    
    </html>

       2.setInterval()、clearInterval(var param)

    • setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式,循环调用
    • clearInterval(var param) 方法可取消由 setInterval() 函数设定的定时执行操作,参数必须是由 setInterval() 返回的 ID 值
    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style>
                #mytime {
                    background: #bbb;
                    color: #fff;
                    display: block;
                }
                
                .wrapper {
                    text-align: center;
                    width: 60%;
                    margin: 250px auto;
                }
            </style>
        </head>
    
        <body>
    
            <div class="wrapper">
                <h1 id=mytime>显示时间</h1>
                <button id="stop" name="button" onclick="stop()">stop</button>
                <button id="start" name="button" onclick="start()">start</button>
                <button id="reset" name="button" onclick="reset()">reset</button>
            </div>
        </body>
        <script type="text/javascript">
            var h = m = s = ms = 0; //定义时,分,秒,毫秒并初始化为0;
            var time = 0;
    
            function timer() { //定义计时函数
                ms = ms + 50; //毫秒
                if(ms >= 1000) {
                    ms = 0;
                    s = s + 1; //
                }
                if(s >= 60) {
                    s = 0;
                    m = m + 1; //分钟
                }
                if(m >= 60) {
                    m = 0;
                    h = h + 1; //小时
                }
                str = toDub(h) + "" + toDub(m) + "" + toDub(s) + "" + toDubms(ms) + "毫秒";
                mytime = document.getElementById('mytime');
                mytime.innerHTML = str;
            }
    
            function reset() { //重置
                clearInterval(time);
                h = m = s = ms = 0;
                document.getElementById('mytime').innerHTML = "00时00分00秒0000毫秒";
                document.getElementById("start").removeAttribute("disabled");
                document.getElementById("stop").setAttribute("disabled", true);
            }
    
            function start() { //开始
                time = setInterval(timer, 50);
                document.getElementById("start").setAttribute("disabled", true);
                document.getElementById("stop").removeAttribute("disabled");
            }
    
            function stop() { //暂停
                clearInterval(time);
                document.getElementById("start").removeAttribute("disabled");
                document.getElementById("stop").setAttribute("disabled", true);
            }
    
            function toDub(n) { //补0操作
                if(n < 10) {
                    return "0" + n;
                } else {
                    return "" + n;
                }
            }
    
            function toDubms(n) { //给毫秒补0操作
                if(n < 10) {
                    return "00" + n;
                } else {
                    return "0" + n;
                }
    
            }
        </script>
    
    </html>
  • 相关阅读:
    自编码器AutoEncoder,降噪自编码器DAE,稀疏自编码器SAE,变分自编码器VAE 简介
    经验模式分解EMD与集合经验模态分解EEMD
    Adversarial Faces
    网络权重初始化方法 常数初始化、Lecun、Xavier与He Kaiming
    信息熵、交叉熵、KL散度、JS散度、Wasserstein距离
    神经网络前向传播和反向传播公式 详细推导
    Softmax 原理及 Sigmoid和Softmax用于分类的区别
    However, but, yet, while, whereas 表转折的区别; while, whereas区别
    阿里云mysql数据库恢复到本地
    js 14位字符串 转日期
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/10169952.html
Copyright © 2011-2022 走看看