1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
function debounce(func, delay, immediate) { var timer = null return function () { var context = this var args = arguments if (timer) clearTimeout(timer) if (immediate) { var doNow = !timer timer = setTimeout(function () { timer = null }, delay) if (doNow) { func.apply(context, args) } } else { timer = setTimeout(function () { func.apply(context, args) }, delay) } } }
var throttle = function (func, delay) { var timer = null var startTime = Date.now()
return function () { var curTime = Date.now() var remaining = delay - (curTime - startTime) var context = this var args = arguments
clearTimeout(timer) if (remaining <= 0) { func.apply(context, args) startTime = Date.now() } else { timer = setTimeout(func, remaining) } } }
|