-
-
Save tphalp/8151085 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Throttle: only execute callback if more than 'ms' has passed since last successful call | |
var throttle = function (ms, func) { | |
var last; | |
return function () { | |
var now = new Date(); | |
if (now - last < ms) { | |
return; | |
} | |
func.apply(this, arguments); | |
last = now; | |
} | |
} | |
// Debounce: only execute callback after more than 'ms' has passed since last call | |
var debounce = function (ms, func) { | |
var timer; | |
return function () { | |
var that = this; | |
var args = arguments; | |
window.clearTimeout(timer); | |
timer = window.setTimeout(function () { | |
func.apply(that, args); | |
}, ms); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment