请用javaScript写出防抖和节流函数
时间: 2023-02-26 21:55:11 浏览: 108
JavaScript函数节流和函数防抖之间的区别.docx
防抖函数:
```
function debounce(fn, wait) {
let timeout;
return function() {
clearTimeout(timeout);
timeout = setTimeout(() => {
fn.apply(this, arguments);
}, wait);
};
}
```
节流函数:
```
function throttle(fn, wait) {
let inThrottle;
return function() {
if (!inThrottle) {
fn.apply(this, arguments);
inThrottle = true;
setTimeout(() => (inThrottle = false), wait);
}
};
}
```
阅读全文