| 1234567891011121314151617181920212223 |
- // 节流函数: 连续不断地触发某事件(如点击),只在单位时间内只触发一次
- // throttle和debounce均是通过减少实际逻辑处理过程的执行来提高事件处理函数运行性能的手段,并没有实质上减少事件的触发次数。
- export default function throttle(fun, delay=2000) {
- let last, deferTimer
- return function (args) {
- let that = this;
- let _args = arguments;
- let now = +new Date();
- if(last && now < last + delay) {
- clearTimeout(deferTimer);
- deferTimer = setTimeout(function () {
- last = now;
- fun.apply(that, _args);
- },delay)
- } else {
- last = now;
- fun.apply(that,_args);
- }
- }
- }
|