页面1分钟内,5秒请求一次接口

前言

功能描述:
1.获取列表参数code:请求A接口(getDataList函数),获取code,code是实时更新的
2. 请求B接口,获取table列表,由于后端是调第三方接口,无法返回一个状态,标记是否给前端返会数据还是空数据
3. 1分钟内,5秒钟调一次接口,直到请求到数据后,清楚定时器

data() {
	return {
		codeTimer: null,//定时器
		codeTime: 12, // 限制1分钟后没请求到erp数据,不再继续请求
		searchEmptyText: '暂无数据',
		isClickQueary: false //是否查询

	};
},
methods: {
	// 查询
	getFilterParams() {
	    **//初始化定时器的值,不然上次请求到数据后,开启定时器codeTime等于0**
		this.codeTime = 12;
		this.getDataList();
	},
	// 获取erp的code
	getDataList() {
		this.tableConfig.loading = true;
		//清空表格提示
		this.searchEmptyText = '';
		const params = {
			pageNumber: 1,
			pageSize: 10
		};
		API.getAccountsPayableCode(params)
			.then(res => {
				if (res.status) {
					this.erpId = res.data;
					//请求列表的接口,同时开启定时器
					this.getErpTableDataList(); 
					this._codeCount();
				}
			})
			.catch(err => {
				this.tableConfig.loading = false;
				this.$message.error(err.msg || '请求失败');
			});
	},
	// 列表
	getErpTableDataList() {
		const params = {
			zzid: this.erpId,
			pageNumber: 1,
			pageSize: 10
		};
		params.pageNumber = this.tableConfig.pageOption.pageIndex;
		params.pageSize = this.tableConfig.pageOption.pageSize;
		API.getSelectAccountsPayableList(params)
			.then(res => {
				this.tableConfig.data = res.data.records;
				this.tableConfig.total = res.data.total;
				**//直到请求到数据,清楚定时器**
				if (res.status && res.data && res.data.records.length !== 0) {
					this._clearCount();
					this.tableConfig.loading = false;
				}
			})
			.catch(err => {
				this.tableConfig.loading = false;
				this.$message.error(err.msg || '请求失败');
			});
	},
	_codeCount() {
		this.codeTimer = window.setInterval(() => {
			if (this.codeTime <= 0) {
				console.log('清楚定时器');
				window.clearInterval(this.codeTimer);
				this.codeTimer = null;
				this.codeTime = 0;
				this.tableConfig.loading = false;
				if (this.tableConfig.data.length === 0) {
					this.searchEmptyText = this.isClickQueary ? '未查询到数据' : '暂无数据';
				}
			} else {
				this.getErpTableDataList();
				this.codeTime--;
			}
			console.log(this.codeTime, 'this.codeTime');
		}, 5000);
	},
	//清楚定时器
	_clearCount() {
		window.clearInterval(this.codeTimer);
		console.log(window, 'window');
		this.codeTimer = null;
		this.codeTime = 0;
	},
	// 分页
	nextPage(page) {
	   **// 每次分页请求接口,需要回复初始值**
		this.codeTime = 12;
		this.tableConfig.pageOption.pageIndex = page.pageIndex;
		this.tableConfig.pageOption.pageSize = page.pageSize;
		this.tableConfig.pageOption.start = (page.pageIndex - 1) * page.pageSize;
		this.getDataList();
	},
}

总计:1.多次开启定时器,关闭定时器,需要一个值来记录判断 2.查询和分页时,初始化记录定时器的codeTime=12 ,不然上次已经请求到数据codeTime是0,开启定时器就直接关闭了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 项目中,可以通过 Vue 的生命周期钩子函数 `beforeDestroy` 来取消上一次接口请求。在 `beforeDestroy` 钩子函数中,可以调用取消请求的方法,例如使用 Axios 的 `cancel` 方法来取消请求。 下面是一个示例代码: ```javascript import axios from 'axios'; export default { data() { return { request: null, // 保存请求的变量 }; }, methods: { fetchData() { // 取消上一次请求 if (this.request) { this.request.cancel('取消上一次请求'); } // 发起新的请求 this.request = axios.CancelToken.source(); // 创建 cancel token axios.get('/api/data', { cancelToken: this.request.token }) .then(response => { // 处理响应数据 }) .catch(error => { if (axios.isCancel(error)) { console.log('请求被取消', error.message); } else { console.log('请求发生错误', error); } }); }, }, beforeDestroy() { // 在页面销毁前取消请求 if (this.request) { this.request.cancel('组件销毁,取消请求'); } }, }; ``` 在这个示例中,我们通过创建一个 CancelToken 对象来生成一个 cancel token,并将其传递给 Axios 的 `cancelToken` 配置项。当需要取消请求时,调用 `cancel` 方法,并传递取消原因。 在 `beforeDestroy` 钩子函数中,我们通过判断是否存在请求对象来决定是否取消请求。当组件即将销毁时,会调用 `beforeDestroy` 钩子函数,此时可以取消上一次请求。 请注意,这里使用的是 Axios 库作为示例,实际项目中可能使用的是其他 HTTP 请求库,取消请求的方法可能会有所不同。具体使用方法请参考对应库的文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值