functionthrottle(fn, delay) { let timer; returnfunction () { let context = this; let args = arguments; if (timer) { return; } timer = setTimeout(function () { fn.apply(context, args); timer = null; }, delay); }; }
使用时间戳实现节流
1 2 3 4 5 6 7 8 9 10 11 12
functionthrottle(fn, delay) { let pre = 0; returnfunction () { let now = newDate(); let context = this; let args = arguments; if (now - pre > delay) { fn.apply(context, args); pre = now; } }; }