1. Create the common.js file in the utils folder
/** ** function anti-shake */
export function debounce (fn, delay) {
// Record the last delay
var timer = null
var delay1 = delay || 200
return function () {
var args = arguments
var that = this
// Clear the last delay
clearTimeout(timer)
timer = setTimeout(function () {
fn.apply(that, args)
}, delay1)
}
}
Copy the code
Introduce this anti-shake kit
- The reason I didn’t use the arrow function here is because there’s a problem with this pointing to so I use the arrow function
// Introduce anti-shake components
import { debounce } from '.. /utils/common'
Copy the code
Use this for shock control
// Real-time search
onInput: debounce(async function () {
const res = await GetTimeData({
keyword: this.inputVal
})
if (res.errno === 0) {
this.listArr = res.data
}
}, 200)
` `List of topics: juejin, github, smartblue, cyanosis, channing-cyan, fancy, hydrogen, condensed-night-purple, greenwillow, v-green, Vue-pro, Healer-readable, Mk-cute, Jzman, Geek-black, awesome-green, qklhK-chocolate https://github.com/xitu/juejin-markdown-themes theme: juejin highlight: ---Copy the code