Skip to content

Instantly share code, notes, and snippets.

@tphalp
Forked from jasonrhodes/fn.timing.js
Created December 27, 2013 18:52
Show Gist options
  • Save tphalp/8151085 to your computer and use it in GitHub Desktop.
Save tphalp/8151085 to your computer and use it in GitHub Desktop.
// 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