zoukankan      html  css  js  c++  java
  • settimeout和setInterval的使用

    1、settimeout

    var timeOut=setOutout(()=>{
                console.log("我是定时器");
            },2000)
            //此时打印的是定时器的id值timeOut和定时器内部数据
            console.log(timeOut);
            //加上清除后,不会打印内部数据,只会打印id值timeOut
            clearTimeout(timeOut);

    2、setInterval

    var a = 0;
            //设置每1s执行一次;
            var timeInt = setInterval(() => {
                a++;
                console.log(a);
                if (a > 5) {
                    //当a>5时清除定时器
                    clearInterval(timeInt);
                }
            }, 1000);

    3、在vue中使用

    var timeOut=setTimeout(function() {
                        console.log("定时器");
                        this.bool=false;//在data中设置动态值bool控制开关
                        console.log(this.bool);
                    }, 2000);
                    if(!this.bool){//判断清除定时器
                        clearTimeout(timeOut);
                    }
    
                    var timeInt = setInterval(() => {
                        console.log("2s打印一次");
                        this.bool = false;
                        if (!this.bool) {
                            clearInterval(timeInt);
                        }
                    }, 2000)
  • 相关阅读:
    CSS margin合并
    最大网络流
    js——this
    js——作用域和闭包
    CSS弹性(flexible)盒子
    CSS盒子模型
    修改html中button显示的文字
    远程唤醒UP Board
    UP Board 串口使用心得
    UP Board 网络设置一本通
  • 原文地址:https://www.cnblogs.com/Alex-Song/p/12156610.html
Copyright © 2011-2022 走看看