|
本帖最后由 OK论坛 于 2024-8-12 10:44 编辑
- //去抖函数(用于限制函数被调用的次数)
- function debounce(func, delay) {
- let timer;
- return function() {
- clearTimeout(timer);
- timer = setTimeout(func, delay);
- };
- }
复制代码- //节流函数(用于限制函数的调用速率):
- function throttle(func, limit) {
- let throttled = false;
- return function() {
- if (!throttled) {
- func();
- throttled = true;
- setTimeout(() => (throttled = false), limit);
- }
- };
- }
复制代码
|
|