vue-节流跟防抖函数

mac2024-05-10  34

1、防抖函数,避免在一定的时间内重复执行

在vue中,提供了lodash库中的_.debounce函数,传入的第一个函数为需要防抖的函数,第二个参数为防抖时间,返回一个已经去抖的函数

<script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script> created: function () {   // `_.debounce` 是一个通过 Lodash 限制操作频率的函数。   // 在这个例子中,我们希望限制访问 yesno.wtf/api 的频率   // AJAX 请求直到用户输入完毕才会发出。想要了解更多关于 // `_.debounce` 函数 (及其近亲 `_.throttle`) 的知识, // 请参考:https://lodash.com/docs#debounce this.debouncedGetAnswer = _.debounce(this.getAnswer, 500) },

2、节流函数,限制一定时间内执行的次数

可以使用lodash中的_.throttle函数,传入的第一个函数为需要节流的函数,第二个函数为限制的时间,返回一个已经节流的函数

最新回复(0)