var timeout1;
//防抖
export function debounce(func, wait) {
if(!Object.prototype.toString.call(func) === '[object Function]'){
console.error("func不是函数对象!")
return false;
}
return function() {
var args = Array.from(arguments);
var content = this;
clearTimeout(timeout1);
timeout1 = setTimeout(func.apply(content, args), wait);
}
}
//节流
var timeout2
export function throttle(func, wait) {
if(!Object.prototype.toString.call(func) === '[object Function]'){
console.error("func不是函数对象!")
return false;
}
return function() {
var args = Array.from(arguments);
var content = this;
timeout2 = setTimeout(
function() {
func.apply(content, args);
clearTimeout(timeout2)
}, wait);
}
}
防抖节流函数
最新推荐文章于 2024-07-25 15:46:04 发布