export function debounce(func, delay, immediate = true) {
let timer = null
return function(args) {
let _this = this
if (timer) {
clearTimeout(timer)
}
if (immediate) {
let now = !timer
timer = setTimeout(() => {
timer = null
}, delay)
now && func.call(_this, args)
} else {
timer = setTimeout(() => {
timer = null
func.call(_this, args)
}, delay)
}
}
}
export function throttle(func, delay, immediate = true) {
let timer = null
return function (args) {
let _this = this
if (!timer) {
if (immediate) {
func.call(_this, args)
timer = setTimeout(() => {
timer = null
}, delay)
} else {
timer = setTimeout(() => {
func.call(_this, args)
timer = null
}, delay)
}
}
}
}