vue项目进行防抖实现请求频繁时取消上一次操作

一、 需求问题:在项目开发中,可能会连续发送多个请求,但是我们只想发送一次请求。比如在搜索框进行输入内容的时候,当在每输入一个字符的时候都会进行搜索查询,频繁发送请求,对服务端造成不必要的请求压力,这就需要在发送新请求的时候取消上一次的操作。只有当所有的内容都输入完毕以后,再进行内容的搜素查询请求。这个也是防抖的常用情景。

二、 需求分析:针对在vue项目中,我们进行防抖,请求频繁时取消上一次的操作,可以通过两种方式。第一种通过clearTimeout()setTimeout()定时器进行解决,第二种可以通过axios进行防抖解决。我们可以看一下在防抖前和防抖后的network变化。
1. 在防抖前,network中在输入内容时会频繁请求,如图所示:
在这里插入图片描述

  1. 在防抖后,network中在输入内容时只会请求最后一次,之前的请求都会被取消掉,如图所示:
    在这里插入图片描述

三、 需求实现

<template>
    <div class="search_body">
        <div class="search_input">
            <div class="search_input_wrapper">
                <i class="iconfont icon-sousuo"></i>
                <input type="text" v-model="message">
            </div>					
        </div>
        <div class="search_result">
            <h3>电影/电视剧/综艺</h3>
            <ul>
                <li v-for="item in movieList" :key="item.id"> 
                    <div class="img"><img :src="item.img | setWH('128.180')"></div>
                    <div class="info">
                        <p><span>{{ item.nm }}</span><span>{{ item.sc }}</span></p>
                        <p>{{ item.enm }}</p>
                        <p>{{ item.cat }}</p>
                        <p>{{ item.rt }}</p>
                    </div>
                </li>
            </ul>
        </div>
	</div>
</template>
    
<script>
export default {
    name: 'Search',
    data() {
        return {
            message: '',
            movieList: []
        }
    },
    methods: {
        // 取消请求的方法
        cancelRequest(){
            if(typeof this.source ==='function'){
                this.source('终止请求')
            }
        }
    },
    watch: {
        message(newVal) {
            // console.log(newVal);
            var that = this;
            this.cancelRequest();

            this.axios.get('/api/searchList?cityId=10&kw='+newVal, {
                cancelToken: new this.axios.CancelToken(function(c) {
                    that.source = c;
                })
            }).then((res) => {
                var msg = res.data.msg;
                var movies = res.data.data.movies;
                if( msg && movies ){
                    this.movieList = res.data.data.movies.list;
                }
            }).catch((err) => {
                if (this.axios.isCancel(err)) {
                    console.log('Rquest canceled', err.message); //请求如果被取消,这里是返回取消的message
                } else {
                    //handle error
                    console.log(err);
                }
            });
        }
    }
}
</script>

<style scoped>
#content .search_body{ flex:1; overflow:auto;}
.search_body .search_input{ padding: 8px 10px; background-color: #f5f5f5; border-bottom: 1px solid #e5e5e5;}
.search_body .search_input_wrapper{ padding: 0 10px; border: 1px solid #e6e6e6; border-radius: 5px; background-color: #fff; display: flex; line-height: 20px;}
.search_body .search_input_wrapper i{font-size: 16px; padding: 4px 0;}
.search_body .search_input_wrapper input{ border: none; font-size: 13px; color: #333; padding: 4px 0; outline: none; margin-left: 5px; width:100%;}
.search_body .search_result h3{ font-size: 15px; color: #999; padding: 9px 15px; border-bottom: 1px solid #e6e6e6;}
.search_body .search_result li{ border-bottom:1px #c9c9c9 dashed; padding: 10px 15px; box-sizing:border-box; display: flex;}
.search_body .search_result .img{ width: 60px; float:left; }
.search_body .search_result .img img{ width: 100%; }
.search_body .search_result .info{ float:left; margin-left: 15px; flex:1;}
.search_body .search_result .info p{ height: 22px; display: flex; line-height: 22px; font-size: 12px;}
.search_body .search_result .info p:nth-of-type(1) span:nth-of-type(1){ font-size: 18px; flex:1; }
.search_body .search_result .info p:nth-of-type(1) span:nth-of-type(2){ font-size: 16px; color:#fc7103;}
</style>