Skip to content

Commit

Permalink
Merge pull request sindresorhus#2 from component/rewrite/use-undersco…
Browse files Browse the repository at this point in the history
…re-implementation

Rewrite: use underscore.js implementation
  • Loading branch information
TooTallNate committed Jun 21, 2014
2 parents b6fbc60 + 31841e5 commit bfbc462
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ function resize(e) {
## License

MIT

Original implementation is from [`underscore.js`](http:https://underscorejs.org/)
which also has an MIT license.
4 changes: 3 additions & 1 deletion component.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"throttle",
"invoke"
],
"dependencies": {},
"dependencies": {
"Raynos/date-now": "v1.0.1"
},
"development": {},
"license": "MIT"
}
49 changes: 35 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@

/**
* Module dependencies.
*/

var now = require('date-now');

/**
* Debounces a function by the given threshold.
* Returns a function, that, as long as it continues to be invoked, will not
* be triggered. The function will be called after it stops being called for
* N milliseconds. If `immediate` is passed, trigger the function on the
* leading edge, instead of the trailing.
*
* @source underscore.js
* @see http:https://unscriptable.com/2009/03/20/debouncing-javascript-methods/
* @param {Function} function to wrap
* @param {Number} timeout in ms (`100`)
* @param {Boolean} whether to execute at the beginning (`false`)
* @api public
*/

module.exports = function debounce(func, threshold, execAsap){
var timeout;
module.exports = function debounce(func, wait, immediate){
var timeout, args, context, timestamp, result;
if (null == wait) wait = 100;

return function debounced(){
var obj = this, args = arguments;
function later() {
var last = now() - timestamp;

function delayed () {
if (!execAsap) {
func.apply(obj, args);
}
if (last < wait && last > 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
if (!timeout) context = args = null;
}
}
};

if (timeout) {
clearTimeout(timeout);
} else if (execAsap) {
func.apply(obj, args);
return function debounced() {
context = this;
args = arguments;
timestamp = now();
var callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}

timeout = setTimeout(delayed, threshold || 100);
return result;
};
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"throttle",
"invoke"
],
"dependencies": {},
"dependencies": {
"date-now": "1.0.1"
},
"devDependencies": {
"mocha": "*",
"should": "*"
Expand Down

0 comments on commit bfbc462

Please sign in to comment.