1
\$\begingroup\$

I've written a quick little helper for event & call debounce/throttling. Since I'm at home and none of my regular code review friends are on-line I figured I'd turn to the great folks here! Would love any feedback you might have.

/**
 * debounce
 * @param {integer} milliseconds This param indicates the number of milliseconds
 * to wait after the last call before calling the original function .
 * @return {function} This returns a function that when called will wait the
 * indicated number of milliseconds after the last call before
 * calling the original function.
 */
Function.prototype.debounce = function (milliseconds) {
 var baseFunction = this,
 timer = null,
 wait = milliseconds;
 return function () {
 var self = this,
 args = arguments;
 function complete() {
 baseFunction.apply(self, args);
 timer = null;
 }
 if (timer) {
 clearTimeout(timer);
 }
 timer = setTimeout(complete, wait);
 };
};
/**
* throttle
* @param {integer} milliseconds This param indicates the number of milliseconds
* to wait between calls before calling the original function.
* @return {function} This returns a function that when called will wait the
* indicated number of milliseconds between calls before
* calling the original function.
*/
Function.prototype.throttle = function (milliseconds) {
 var baseFunction = this,
 lastEventTimestamp = null,
 limit = milliseconds;
 return function () {
 var self = this,
 args = arguments,
 now = Date.now();
 if (!lastEventTimestamp || now - lastEventTimestamp >= limit) {
 lastEventTimestamp = now;
 baseFunction.apply(self, args);
 }
 };
};

To help understand what the point of these helpers are I've prepared this demo: http://jsfiddle.net/zR5jV/1/

GitHub project can be found here: https://github.com/m-gagne/limit.js

asked Apr 16, 2012 at 21:05
\$\endgroup\$
2
  • \$\begingroup\$ Honestly, you'd probably be better off just using underscore.js, rather than reinventing the wheel (unless you're doing it for fun). At just around 4K minified, it's a very powerful library that includes more refined versions of both of these methods and is complimentary to almost every other JavaScript library. \$\endgroup\$ Commented Apr 26, 2012 at 19:46
  • \$\begingroup\$ Cool hadn't heard of underscore.js before, thanks. Yes this was more of a "do it for fun" / "try creating a project & associated website" project. It was also more to bring to light the issue of frequency of events that people might not know of. Rarely as a developer do we ever get to create something truly unique, but I've learned a lot and gotten some great feedback in producing this simple snippet of code :) \$\endgroup\$ Commented Apr 27, 2012 at 15:20

1 Answer 1

2
\$\begingroup\$

The key differences between a throttled function and a debounced function are that the throttled function can return a value because it is called synchronously and the debounced version cannot because it is used asynchronously.

You have lost that on your throttle implementation. I would at least make the following change:

Function.prototype.throttle = function (milliseconds) {
 var baseFunction = this,
 lastEventTimestamp = null,
 limit = milliseconds,
 lastresult;
 return function () {
 var self = this,
 args = arguments,
 now = Date.now();
 if (!lastEventTimestamp || now - lastEventTimestamp >= limit) {
 lastEventTimestamp = now;
 lastresult = baseFunction.apply(self, args);
 }
 return lastresult;
 };
};
answered Apr 24, 2012 at 20:27
\$\endgroup\$
1
  • \$\begingroup\$ Good point, I've only used throttle in scenarios where I don't care about the return value. I'll integrate this into my library, thanks! \$\endgroup\$ Commented Apr 25, 2012 at 12:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.