Vue方法使用防抖和节流
最简单的一种:直接使用lodash封装好的工厂函数:
-
// _为自己命名
-
import _ from 'lodash'
-
-
// debounce 防抖
-
方法名:_.debounce(function(传递参数){
-
//具体方法
-
},300),
-
-
// throttle 节流
-
方法名:_.throttle(function(传递参数){
-
//具体方法
-
},300),
会返回一个新的函数,直接在vue的methods中使用:
-
methods: {
-
// 改变场数
-
changefield: _.debounce(function(_type, index, item) {
-
// do something ...
-
}, 300)
-
}
或者:
-
methods:{
-
queryList() {
-
return _.debounce(() => {
-
// 函数体
-
}, 500)()
-
}
-
}
自己封装一个防抖和节流的工具类:
-
// 防抖
-
export function _debounce(fn, delay) {
-
-
var delay = delay || 200;
-
var timer;
-
return function () {
-
var th = this;
-
var args = arguments;
-
if (timer) {
-
clearTimeout(timer);
-
}
-
timer = setTimeout(function () {
-
timer = null;
-
fn.apply(th, args);
-
}, delay);
-
};
-
}
-
// 节流
-
export function _throttle(fn, interval) {
-
var last;
-
var timer;
-
var interval = interval || 200;
-
return function () {
-
var th = this;
-
var args = arguments;
-
var now = new Date();
-
if (last && now - last < interval) {
-
clearTimeout(timer);
-
timer = setTimeout(function () {
-
last = now;
-
fn.apply(th, args);
-
}, interval);
-
} else {
-
last = now;
-
fn.apply(th, args);
-
}
-
}
-
}
然后在vue中引入并使用:
-
import { _debounce } from "..."
-
-
-
-
-
methods: {
-
// 改变场数
-
changefield: _debounce(function(_type, index, item) {
-
// do something ...
-
}, 200)
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhiegcah
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
excel下划线不显示怎么办
PHP中文网 06-23 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22 -
excel打印预览压线压字怎么办
PHP中文网 06-22