// tools.js
export function debounce (fn, delay) {
let timer = null
return function () {
let arg = arguments
clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, arg)
}, delay)
}
}
复制代码
// App.vue
<template>
<div id="app">
<input v-model="text" type="text">
</div>
</template>
<script>
export default {
name: 'App',
data () {
return {
text: ''
}
},
created () {
this.$watch('text', debounce((newValue, oldValue) => {
console.log(newValue)
}, 300))
}
}
</script>
复制代码
所有评论(0)