const getData = async () => {
setLoading(true);
const currentRequest = Date.now();//加一个时间戳
latestRequestRef.current = currentRequest;//将刚才的时间戳保存到最后一次被点击的钩子函数里面
PerformanceApi.getHistoryPerformance({
assessFormId: assessFormId,
cycletype: cycleType < 0 ? '' : cycleType,
})
.then((res: any) => {
if (res.Code) {
//如果两个相等,说明就是本次的,最新点击的一次获取数据,否则jiu
if (latestRequestRef.current === currentRequest) {
setData(res.Data);
}
}
if (cycleType < 0) {
isPreventMultipleCalls.current = true;
setCycleType(res.Data.formTags[0].cycleType);
}
setLoading(false);
})
.catch((e) => console.log(e));
};
防止接口返回数据慢,点击其他按钮请求数据之后被返回慢的数据覆盖掉新数据
最新推荐文章于 2024-09-08 21:17:37 发布
本文介绍了一个使用JavaScript异步函数`getData`的方法,通过`PerformanceApi`获取历史性能数据,确保只有最后一次点击才会触发数据更新,并在特定条件下防止多次请求。当请求成功时,更新数据并处理可能的错误。
摘要由CSDN通过智能技术生成