vue使用setInterval定时器

617 阅读1分钟

setInterval定时器单位为毫秒

<script>
  export default {
    data() {
      return {
        timer: null,
      }
    },
    beforeDestroy() {
      // 清除定时器
      clearInterval(this.timer);
    },
    mounted() {
      this.getDataList();
      // 每10秒调用一次接口
      this.timer = setInterval(() => {
        this.getDataList();
      }, 10000);
    },
    methods: {
      getDataList() {
        console.log('调用了');
      }
    }
  }
</script>