每隔一段时间向后台请求一次数据

每隔多长时间向后台请求一次数据

1、在mounted方法中定义一个定时器,每隔多长时间去触发一下

mounted() {
    clearInterval(this.timer);
    this.timer = setInterval(() => {
      let newTime = new Date().getTime();
      store.commit('user/UPDATE_Time', newTime);//在store下面的user模块中,实时的改变的时间
    }, setTime * 1000 * 60);
  },
  destroyed() {
    clearInterval(this.timer);
  }

2、store文件下的user.js代码

import api from '@/assets/api';
export default {
  namespaced: 'user',
  state: {
    // token信息
    token: 'null',
    newTime: '',
  },
  mutations: {
    // 更新TOKEN
    UPDATE_TOKEN(state, data) {
      state.token = data;
    },
    //可以实时的更新时间
    UPDATE_Time(state, data) {
      state.newTime = data;
    },
  },
  actions: {},
};

3、在各个页面中去监听时间的变化,当时间有变化的时候,则去向后台发送请求,请求数据‘

computed: {
    isFollow() {
      return store.state.user.newTime; //需要监听的数据
    },
  },
  watch: {
    isFollow(newVal, oldVal) {
    //当时间发生变化的时候,isFollow也会随之变化,此时调用请求数据的方法即可
     this.reqEquimentDis();
    },
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值