FlowPacs/yudao-vue-ui/common/js/util.js

47 lines
890 B
JavaScript
Raw Normal View History

2021-11-25 07:57:23 +08:00
let _debounceTimeout = null,
_throttleRunning = false
/**
* 防抖
* 参考文章 https://juejin.cn/post/6844903669389885453
*
* @param {Function} 执行函数
* @param {Number} delay 延时ms
*/
export const debounce = (fn, delay=500) => {
clearTimeout(_debounceTimeout);
_debounceTimeout = setTimeout(() => {
fn();
}, delay);
}
/**
* 节流
* 参考文章 https://juejin.cn/post/6844903669389885453
*
* @param {Function} 执行函数
* @param {Number} delay 延时ms
*/
export const throttle = (fn, delay=500) => {
if(_throttleRunning){
return;
}
_throttleRunning = true;
fn();
setTimeout(() => {
_throttleRunning = false;
}, delay);
}
/**
* toast
*/
export const msg = (title = '', param={}) => {
if(!title) return;
uni.showToast({
title,
duration: param.duration || 1500,
mask: param.mask || false,
icon: param.icon || 'none'
});
}