关于vue项目中搜索节流的实现

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_39045645/article/details/93852538

在这里插入图片描述
我们经常会遇到这种需求,现在我们在使用百度搜索的时候他们的思想也是
根据防抖节流而实现的,至于用防抖还是节流根据自己需求。

<template>
 <input type="text"   v-model.trim="sse">
</template>
<script>
const delay = (function () {
  let timer = 0
  return function (callback, ms) {
    clearTimeout(timer)
    timer = setTimeout(callback, ms)
  }
})()
export default {
	name :  'search',
	watch : {
		sse () {
	 delay(() => {
        this.search()
      }, 500)
},
methods :{
	search () {
        this.$axios
          .get([url])
          .then(response => {
            // success
          })
          .catch(error => {
            // error
            alert('失败!')
          })
}
}
}
}

</script>


猜你喜欢

转载自blog.csdn.net/qq_39045645/article/details/93852538