• 计时器——块移动


     1 <!doctype html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Document</title>
     6     <style>
     7     *{margin: 0;padding: 0;}
     8     #con{width: 100px;height: 100px;background: red;position: absolute;left: 0;top: 100px;}
     9     #but{width: 30px;height: 30px;}
    10 
    11     </style>
    12 </head>
    13 <body>
    14     <input id="but" type="button" value="go">
    15     <input id="but1" type="button" value="stop">
    16     <div id="con"></div>
    17     
    18     <script>
    19     /*function as() {
    20         alert('aa');
    21     };
    22     setInterval(as,100)*/
    23     var con=document.getElementById('con');
    24     var but=document.getElementById('but');
    25     var but1=document.getElementById('but1');
    26     var l=0;
    27     var timer=null,time2=null,time3=null;
    28     but.onclick=function() {
    29         clearInterval(timer);
    30         //每次点击的时候先清掉计时器,再去执行,防止触发多个计时器造成页面混乱
    31         timer=setInterval(function() {
    32             l+=10;
    33             con.style.left=l+'px';
    34         },100)
    35     };
    36     but1.onclick=function() {
    37         clearInterval(timer);
    38     };
    39     // time2=setTimeout(function() {
    40     //     alert('aa');
    41     // },1000)
    42 //timeout一次性的计时器可以当做延时效果来用
    43     </script>
    44 </body>
    45 </html>
    View Code
  • 相关阅读:
    JZOJ5809 数羊
    P3313 [SDOI2014]旅行
    2019.10.22 NOIP模拟测试 day2
    P4322 [JSOI2016]最佳团体
    P1850 换教室
    P3225 [HNOI2012]矿场搭建
    P2607 [ZJOI2008]骑士
    2019.10.21 NOIP模拟测试 day1
    AFO
    禁止加载浏览器图片
  • 原文地址:https://www.cnblogs.com/yoyoyoyoyoyo/p/5815550.html
Copyright © 2020-2023  润新知