1、在utils文件夹下创建tool.ts工具类文件
export function debounceFun(fn, wait = 600) {
let _this = this as any
let args = arguments
return function () {
if (_this.timeout) {
clearTimeout(_this.timeout)
}
_this.timeout = setTimeout(() => {
fn.apply(_this, args)
}, wait)
}
}
export function throttleFun(func,delay = 600){
let _this = this as any
let args = arguments
return function(){
if (_this.timeout) {
return
}
_this.timeout = setTimeout(() => {
func.apply(_this, args)
_this.timeout = null
},delay)
}
}
<template>
<div class="box">
<a-input
v-model="userName"
@change="changeValue()"
/>
</div>
</template>
<script lang="tsx">
import Component from 'vue-class-component'
import { Vue } from 'vue-property-decorator'
import { debounceFun } from "@/utils/tool"
@Component({
components: {}
})
export default class Main extends Vue {
userName: string = ''
changeValue(){
debounceFun.bind(this)(() => {
console.log(this.userName)
}, 1000)()
}
}
</script>