This commit is contained in:
Max Lynch
2015-08-28 15:55:44 -05:00
parent 0078e4d4b7
commit 715680ac3f
15 changed files with 113 additions and 44 deletions

View File

@@ -51,6 +51,31 @@ function _baseExtend(dst, objs, deep) {
return dst;
}
export function debounce(func, wait, immediate) {
var timeout, args, context, timestamp, result;
return function() {
context = this;
args = arguments;
timestamp = new Date();
var later = function() {
var last = (new Date()) - timestamp;
if (last < wait) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) result = func.apply(context, args);
}
};
var callNow = immediate && !timeout;
if (!timeout) {
timeout = setTimeout(later, wait);
}
if (callNow) result = func.apply(context, args);
return result;
};
}
/**
* Apply default arguments if they don't exist in
* the first object.