1、防抖
let _debounceTimeout = null
/**
* 防抖
* @param {Function} 执行函数
* @param {Number} delay 延时ms
*/
export const debounce = (fn, delay = 500) => {
clearTimeout(_debounceTimeout);
_debounceTimeout = setTimeout(() => {
fn();
}, delay);
}
2、节流
let _throttleRunning = false
/**
* 节流
* @param {Function} 执行函数
* @param {Number} delay 延时ms
*/
export const throttle = (fn, delay = 500) => {
if (_throttleRunning) {
return;
}
_throttleRunning = true;
fn();
setTimeout(() => {
_throttleRunning = false;
}, delay);
}
3、文件下载
/**
* @description: 文件下载
* @param {*} blob
* @param {*} name 文件名称
* @return {*}
* @author: gumingchen
*/
export function download (blob, name) {
if (blob) {
const href = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = href
a.download = name
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
window.URL.revokeObjectURL(href)
}
}