多余请求的优化

先看一张图
观察下方数据展示变化

在这个页面里我们有 3 个按钮获取 3 个不同异步回调数据,下方使用同一个容器展示。在快速点击按钮 2、3 时,按钮 2 的异步请求还在 Pending,按钮 3 的异步请求也已发出,在
按钮 2 回调完成时,下方显示按钮 2 的回调数据,过了一会儿按钮 3 回调完成,数据替换为按钮 3 的对应数据。

优化方案 1

为当前请求设置一个标识 Flag,记录当前需要展示哪一个按钮的回调。

代码示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<title>Request Optimization Example</title>
<style>
.btns button {
margin-right: 10px;
}
.result {
margin-top: 20px;
}
</style>
</head>
<body>
<div id="app">
<div class="btns">
<button v-for="index in 3" :key="index" @click="getResult(index)">{{ `获取结果${index}` }}</button>
</div>
<div class="result">
<div v-show="loading">加载中</div>
<div v-show="!loading">{{ result }}</div>
</div>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
loading: false,
result: '',
activeIndex: null
},
methods: {
getResult(index) {
this.loading = true
this.activeIndex = index
setTimeout(() => {
if (index !== this.activeIndex) {
return
}
this.result = `按钮${index}的异步回调结果`
this.loading = false
}, 1000)
}
}
})
</script>
</body>
</html>

下方可以展示正确的回调数据

不足之处:此时虽然可以展示正确的回调结果,但是其实点击按钮的时候依然发出了不必要的异步请求。

优化方案 2

使用 Axios 的 CancelToken 进行取消请求

使用cancelToken取消pending中的请求

完整代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<title>Request Optimization Example</title>
<style>
.btns button {
margin-right: 10px;
}
.result {
margin-top: 20px;
}
</style>
</head>
<body>
<div id="app">
<div class="btns">
<button v-for="index in 3" :key="index" @click="getResult(index)">{{ `获取结果${index}` }}</button>
</div>
<div class="result">
<div v-show="loading">加载中</div>
<div v-show="!loading">{{ result }}</div>
</div>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
loading: false,
result: '',
cancelFetch: null
},
methods: {
getResult(index) {
this.loading = true
if (this.cancelFetch) {
this.cancelFetch()
this.cancelFetch = null
}
axios
.post('https://api.vvhan.com/api/bing?type=json', null, {
cancelToken: new axios.CancelToken(c => {
this.cancelFetch = c
})
})
.then(res => {
this.result = `按钮${index}的异步回调结果`
this.loading = false
})
.catch(() => {})
}
}
})
</script>
</body>
</html>

对于防止重复点击触发事件可以配合Debounce食用

完结,撒花。