对于 js 性能优化的启发,debounce 和 throttle。
debounce: 连续触发多次事件的情况下,只发送一次执行请求。
function deBounceEvent(callback, time) {
let interval;
return function(args) {
clearTimeout(interval);
interval = setTimeout(() => {
interval = null;
callback(args);
}, time);
}
}
throttle: 设置一个周期,在连续处罚多次事件的情况下,每个一短周期执行一次
function throttle(fn, wait) { var time = Date.now(); return function() { if ((time + wait - Date.now()) < 0) { fn(); time = Date.now(); } } }
参考具体动画形式 http://demo.nimius.net/debounce_throttle/