setTimeout() 与setInterval()

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cristina_song/article/details/81837534

setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。
改变this作用域的三种方法(3s置空):
1.绑定

setTimeout(function () {
                this.$store.dispatch("setErrMsg", '');
            }.bind(this), 3000)//绑定外层的this
  1. 箭头函数
setTimeout(() =>{
                this.$store.dispatch("setErrMsg", '');
            }, 3000)
  1. proxy属性,需要引入jquery
setTimeout($.proxy(function () {
                this.$store.dispatch("setErrMsg", '');
            }, this), 3000)//绑定外层的this

setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。 setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。

if (msg.action == "mainteacherstartvote") {
          this.voteTimer = setInterval(() => {
            this.$store.dispatch("voteMethod"); //加载投票结果接口
          }, 5000);
        }
      } else if (msg.action == "mainteacherendvote") {
        //结束投票
        //关闭5s计时
        clearInterval(this.voteTimer);
        console.log("结束投票");
      }

猜你喜欢

转载自blog.csdn.net/cristina_song/article/details/81837534