Use timers in Vue (setInterval, setTimeout)

There are two types of timers in js, one is to execute setInterval cyclically, and the other is to execute setTimeout periodically.

Note: The timer needs to be cleared when the page is destroyed, otherwise it will always exist!

1. Loop execution (setInterval)

As the name implies, loop execution is to set a time interval, and this method will be executed in a loop every time until the timer is destroyed;
syntax :

setInterval(code, milliseconds)setInterval(function, milliseconds, param1, param2, ...);

code/function	        必需。要调用一个代码串,也可以是一个函数。
milliseconds	        必须。周期性执行或调用 code/function 之间的时间间隔,以毫秒计。
param1, param2, ...     可选。 传给执行函数的其他参数(IE9 及其更早版本不支持该参数)。


返回值:	  返回一个 ID(数字),可以将这个ID传递给 clearInterval() 来取消执行。

eg: At the
beginning, a timer setInterval was created with a time interval of 2 seconds. The function valChange will be called every 2 seconds to make the value of value +1.

<template>
  <div>
    <h1>{
    
    {
    
    value}}</h1>
    <el-button type="primary" @click="start">开始</el-button>
    <el-button type="danger" @click="over">结束</el-button>
  </div>
</template>

<script>
export default {
    
    
  data() {
    
    
    return {
    
    
      timer: "",
      value: 0,
    };
  },
  methods: {
    
    
    start(){
    
    
      this.timer = setInterval(this.valChange, 2000); // 注意: 第一个参数为方法名的时候不要加括号;
    },
    valChange() {
    
    
      this.value++;
      console.log(this.value);
    },
    over(){
    
    
      clearInterval(this.timer);
    }
  },
  mounted() {
    
    },
  beforeDestroy() {
    
    
    clearInterval(this.timer);
  },
};
</script>

effect:
Insert picture description here

2. Timed execution (setTimeout)

Timed execution of setTimeout is to set a time, and only execute once when the waiting time is reached , but after the execution, the timer is still there, but it is no longer running;
syntax :

setTimeout(code, milliseconds, param1, param2, ...)
setTimeout(function, milliseconds, param1, param2, ...)

code/function	         必需。要调用一个代码串,也可以是一个函数。
milliseconds	         可选。执行或调用 code/function 需要等待的时间,以毫秒计。默认为 0。
param1, param2, ...	     可选。 传给执行函数的其他参数(IE9 及其更早版本不支持该参数)。

返回值:	返回一个 ID(数字),可以将这个ID传递给 clearTimeout() 来取消执行。

eg:
Create a timer setTimeout at the beginning, and execute the method only after 2 seconds.

<template>
  <div>
    <h1>{
    
    {
    
    value}}</h1>
    <el-button type="primary" @click="start">开始</el-button>
  </div>
</template>

<script>
export default {
    
    
  data() {
    
    
    return {
    
    
      timer: "",
      value: 0,
    };
  },
  methods: {
    
    
    start(){
    
    
      this.timer = setTimeout(this.valChange, 2000); // 注意: 第一个参数为方法名的时候不要加括号;
    },
    valChange() {
    
    
      this.value++;
      console.log(this.value);
    }
  },
  mounted() {
    
    },
  beforeDestroy() {
    
    
    clearTimeout(this.timer);
  },
};
</script>

effect:
Insert picture description here

Guess you like

Origin blog.csdn.net/ZYS10000/article/details/110783815