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

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="UTF-8">
     5         <title></title>
     6     </head>
     7     <body>
     8     </body>
     9         
    10     <script type="text/javascript">
    11         /* 定时器 有俩种*/
    12 
    13         /*第一种:几秒之后执行一次
    14         setTimeout("alert(1)",1000);*/
    15 
    16 
    17 
    18         // 思考:2s之后控制台输出 “hello”
    19          
    20         /* 单引号和双引号
    21          * 共同点:都用于字符串
    22          * 不同点:双引号会解析它里面的变量或者是关键字
    23          * 单引号不会,被单引号包起来的一定是纯字符串
    24          * 单引号在处理纯字符串效率会高一点
    25         */
    26 
    27         /*setTimeout('console.log("hello")',1000);
    28         第二种:每隔几秒执行一次代码*/
    29 
    30 
    31         // 第一个参数:执行的代码   第二个参数:时间间隔
    32         // setInterval('console.log("hello")',1000);
    33         
    34             
    35         /*上述俩种定时器,第一个参数一般都不使用字符串,
    36          * 而是使用匿名函数
    37          * 这个匿名函数会自动被调用*/
    38 
    39 
    40         // 1s 之后执行一次匿名函数
    41         setTimeout(function(){
    42             console.log("hello");
    43         },1000);
    44 
    45 
    46         //    每间隔2s 执行一次匿名函数
    47             setInterval(function(){
    48                 console.log("2s");
    49             },2000);
    50     
    51 
    52         //  移除定时器
    53             var timer = setTimeout(function (){
    54                 console.log("1s后执行一次");
    55             },1000);
    56 
    57         // 移除定时器也就是移除设置定时器时候的变量
    58         //    clearTimeout(timer);
    59         var i = 0;
    60         var timer = setInterval(function (){
    61             i++;
    62             console.log(i);
    63             if(5 == i){
    64             // 移除
    65                 clearInterval(timer);
    66             }
    67         },1000);
    68         
    69         
    70     </script>
    71     
    72 </html>
  • 相关阅读:
    js导出excel增加表头、mso-number-format定义数据格式
    $.each() 与 $(selector).each()的区别
    【Linux常用命令】Linux kill, killall, kill -9,
    Shell 语法报错记录
    【常用脚本记录1----挂测问题脚本】
    【】系统一启动就会运行的程序
    【WAN】PPPOE宽带上网功能详解
    解决linux awk:line的问题
    Ubuntu 16.04安装测试MQTT Mosquitto
    【TCP协议详解】
  • 原文地址:https://www.cnblogs.com/PowellZhao/p/5547141.html
Copyright © 2011-2022 走看看