zoukankan      html  css  js  c++  java
  • JavaScript定时器

    定时器

    开启定时器

    Setinterval间隔型    每隔一段时间重复的执行

    SetTimeout延时型   只执行一次

    两种定时器的区别

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script>
            function show(){
                alert('a');
            }
            setInterval(show,1000);//每隔1000毫秒弹出一个a
        </script>
    </head>
    <body>
    
    </body>
    </html>

    停止定时器

    ClearInterval

    clearTimeout

    <!DOCTYPE html>
    
    <html lang="en">
    
    <head>
    
        <meta charset="UTF-8">
    
        <title>Title</title>
    
        <script>
    
            window.onload=function(){
    
                var oBtn1=document.getElementById('btn1');
    
                var oBtn2=document.getElementById('btn2');
    
                var timer=null;
    
              oBtn1.onclick=function(){
    
                timer=setInterval(function(){
    
                      alert('a');
    
                  },1000);
    
              };
    
              oBtn2.onclick=function(){
    
                  clearInterval(timer);
    
              }
    
            };
    
        </script>
    
    </head>
    
    <body>
    
    <button>
    
        <input id="btn1" type="button" value="开始"/>
    
        <input id="btn2" tyPe="button" value="结束"/>
    
    </button>
    
    </body>
    
    </html>

     

  • 相关阅读:
    php 原生 好久不写原生demo了
    鸡汤
    php 发送smtp邮件
    php微信支付代码
    3、Flume
    P2761 软件补丁问题
    TQL
    二分图匹配
    p2597 灾难
    P3958 奶酪
  • 原文地址:https://www.cnblogs.com/Yimi/p/6056823.html
Copyright © 2011-2022 走看看