vue之input輸入框防抖debounce函數(shù)的使用方式
更新時間:2023年11月17日 10:59:32 作者:如果會御劍
這篇文章主要介紹了vue之input輸入框防抖debounce函數(shù)的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
方法一
這種方式很簡單,但是能實現(xiàn)一樣的功能。
<template>
<div>
<input class="inputBox" type="text" placeholder="搜索項目名稱" v-model="searchValue" @keyup.enter="searchBtn" @input="searchBtn">
</div>
</template><script>
let timers = null;
export default{
data(){
return{
searchValue:''
}
}
methods:{
searchBtn(){
clearTimeout(timers)
timers = setTimeout(() => {
this.getOfferList()//需要防抖的函數(shù)
}, 500);
},
}
}
</script>方法二
這個方法是vue官網(wǎng)上的做法。
<template>
<div>
<input class="inputBox" type="text" placeholder="搜索項目名稱" v-model="searchValue">
</div>
<template><script>
export default{
data(){
return{
searchValue:''
}
}
//監(jiān)聽input輸入值變化
watch:{
searchValue:function(){
this.debouncedGetAnswer();
}
},
created(){
this.debouncedGetAnswer = this.debounce(this.getOfferList, 500);
//this.getOfferList是你查詢數(shù)據(jù)的函數(shù),也就是需要防抖的函數(shù)
},
methods:{
//防抖
debounce(fn, delay = 500){
let timer = null;
return function() {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(this, arguments)
timer = null
}, delay)
}
}
}
</script>方法三
1.封裝一個模塊,引入即可,在utils新建一個js文件,名稱隨便
<template>
<div>
<input class="inputBox" type="text" placeholder="搜索項目名稱" v-model="searchValue">
</div>
<template><script>
export default{
data(){
return{
searchValue:''
}
}
//監(jiān)聽input輸入值變化
watch:{
searchValue:function(){
this.debouncedGetAnswer();
}
},
created(){
this.debouncedGetAnswer = this.debounce(this.getOfferList, 500);
//this.getOfferList是你查詢數(shù)據(jù)的函數(shù),也就是需要防抖的函數(shù)
},
methods:{
//防抖
debounce(fn, delay = 500){
let timer = null;
return function() {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(this, arguments)
timer = null
}, delay)
}
}
}
</script>2.在需要的vue文件引入即可使用
import { _throttle, _debounce } from '@/utils/throttle'3.在vue文件的使用方法
// 監(jiān)聽搜索框內(nèi)容的變化,等輸入完成才會執(zhí)行搜索函數(shù)=>即防抖
watch: {
searchValue: _debounce(function() {
this.page = 1
this.getJobFairList()
})
},
// 搜索,短時間內(nèi)連續(xù)點擊搜索按鈕只執(zhí)行一次搜索函數(shù)=>即節(jié)流
searchBtn: _throttle(function() {
if (this.searchValue) {
this.getJobFairList()
}
}),方法三是最近才更新的代碼,也是最新的代碼,建議使用方法三
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue集成capacitor android并打包為apk實踐
文章主要介紹了如何將Vue項目集成到Capacitor和Android Studio中,并打包成APK文件,過程中遇到了版本沖突、下載超時和Java版本不匹配等問題,并提供了相應的解決方案2026-03-03
vue+webrtc(騰訊云) 實現(xiàn)直播功能的實踐
本文主要介紹了vue+webrtc(騰訊云) 實現(xiàn)直播功能的實踐,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
解決element-ui?el-drawer抽屜el-dialog彈框關閉優(yōu)化demo
這篇文章主要為大家介紹了解決element-ui?el-drawer抽屜el-dialog彈框關閉優(yōu)化demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪<BR>2023-06-06
Vue3中使用Composition API管理組件狀態(tài)
Vue3的CompositionAPI通過reactive、ref、computed、watch等函數(shù),實現(xiàn)更靈活、可組合的狀態(tài)管理,提升邏輯復用與類型推導效率,x下面就來介紹一下如何使用2025-08-08
Springboot運用vue+echarts前后端交互實現(xiàn)動態(tài)圓環(huán)圖
我們做項目的時候,常常需要一些統(tǒng)計圖來展示我們的數(shù)據(jù),作為web開發(fā)人員,會實現(xiàn)統(tǒng)計圖是我們必會的技能。我將帶大家來實現(xiàn)動態(tài)餅圖的實現(xiàn),感興趣的可以了解一下2021-06-06

