源文件

This commit is contained in:
gyq
2024-05-23 14:39:33 +08:00
commit a1128dd791
2997 changed files with 500069 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
/**
* @param {Function} fn 需要执行的方法因this指向问题建议不使用箭头函数
* @param {Number} delay 间隔时间默认值100
* @param {Boolean} promptly 是否立即执行默认false
* **/
export const debounce = (fn, delay = 100, promptly) => {
let timer = null
return function (...args) {
// 立即执行
if (promptly) {
// 当timer为null时执行
if (!timer) fn.apply(this, args)
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
timer = null
}, delay)
} else {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(this, args)
}, delay)
}
}
}
/**
* @param {Function} fn 需要执行的方法因this指向问题建议不使用箭头函数
* @param {Number} delay 间隔时间默认值100
* **/
const throttle = (fn, delay = 100) => {
let valid = true
return function (...args) {
if (!valid) {
return
}
valid = false
fn.apply(this, args)
setTimeout(() => {
valid = true
}, delay)
}
}