// 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>
复制代码
Logo

前往低代码交流专区

更多推荐