防抖动与节流 发表于 2018-01-10 一、防抖动12345678910111213141516171819/** * 防抖动 * @method debounce * @param {function} callback 回调函数 * @param {number} delay 时间间隔 * */export const debounce = function(callback, delay) { let timer = null; return function() { let context = this; let args = arguments; clearTimeout(timer); timer = setTimeout(function() { callback.apply(context, args); }, delay); }}; 二、节流123456789101112131415161718/** * 节流 * @method throttle * @param {function} callback 回调函数 * @param {number} delay 时间间隔 * */export const throttle = function(callback, delay) { let prev = Date.now(); return function() { let context = this; let args = arguments; let now = Date.now(); if (now - prev >= delay) { callback.apply(context, args); prev = Date.now(); } }};