首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VueJS:如果x秒内未收到接口响应,则设置属性

VueJS:如果x秒内未收到接口响应,则设置属性
EN

Stack Overflow用户
提问于 2021-04-30 20:22:22
回答 1查看 18关注 0票数 1

我的VueJS应用程序使用axios向后端发出请求。假设有以下调用:

const response = await ApiService.getDocuments();

响应可能需要一些时间(例如5-10秒),因为服务器还会执行后台处理。现在,我想显示一个加载指示器,但仅当在2秒内没有响应时才显示。因此伪代码可能如下所示:

代码语言:javascript
复制
this.showProgressIndidcator = false;
const response = await ApiService.getDocuments();

// this property shall be set if there is no response within 2 seconds
this.showProgressIndidcator = true;

我该如何用VueJS做什么呢?我们也使用lodash,这可能会提供一些合适的东西?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-30 20:36:35

使用setTimeout,它在2秒后运行

代码语言:javascript
复制
// initial state
this.showProgressIndidcator = false;

// set state after 2 seconds
this.loadingTimer = setTimeout(() => this.showProgressIndidcator = true, 2000);

// call api
const response = await ApiService.getDocuments();

// clear timeout
clearTimeout(this.loadingTimer);

// clear state
this.showProgressIndidcator = false;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67333856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档