Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored for DRYness and added eventData (which I totally forgot ab…

…out)
  • Loading branch information...
commit b6d9187fc48b73fbcaef44f4a9bfe3d1aac28263 1 parent 20d7fdb
Luke Morton authored
Showing with 52 additions and 44 deletions.
  1. +52 −44 jquery.bind.js
96 jquery.bind.js
View
@@ -15,16 +15,7 @@
*
* @author Luke Morton, 2011
* @license MIT
- */
-
-(function ($) {
-
- // Get jQuery duration
- var calcDuration = function (duration) {
- return $.fx.off ? 0 : typeof duration === "number" ? duration :
- duration in $.fx.speeds ? $.fx.speeds[duration] : $.fx.speeds._default;
- };
-
+ */
/**
* Bind on event stop
*
@@ -32,29 +23,12 @@
* occurance of event, then calls the callback.
*
* @param string jQuery event, e.g. 'click', 'keydown'
+ * @param object jQuery event data
* @param callback Callback function
* @param mixed jQuery duration, e.g. 'slow', 'fast'
* or an integer (milliseconds)
* @return this
*/
- $.fn.bindStop = function (event, callback, delay) {
- delay = calcDuration(delay);
-
- return this.each(function () {
- var self = this;
- var timer;
- $(self).bind(event, function (e) {
- if (timer) {
- clearTimeout(timer);
- }
- timer = setTimeout(function () {
- callback.call(self, e);
- }, delay);
- });
- });
-
- };
-
/**
* Throttle bound events
*
@@ -63,30 +37,64 @@
* it waits until after the throttle time to call it.
*
* @param string jQuery event, e.g. 'click', 'keydown'
+ * @param object jQuery event data
* @param callback Callback function
* @param mixed jQuery duration, e.g. 'slow', 'fast'
* or an integer (milliseconds)
* @return this
*/
- $.fn.bindThrottle = function (event, callback, delay) {
- delay = calcDuration(delay);
+
+(function ($) {
+
+ // Get jQuery duration
+ var calcDuration = function (duration) {
+ return $.fx.off ? 0 : typeof duration === "number" ? duration :
+ duration in $.fx.speeds ? $.fx.speeds[duration] : $.fx.speeds._default;
+ };
+
+ // Lets do both at the same time :)
+ $.each(['bindStop', 'bindThrottle'], function (i, name) {
+ $.fn[name] = function (eventType, eventData, handler, delay) {
- return this.each(function () {
- var self = this;
- var timer;
- $(self).bind(event, function (e) {
- if ( ! timer) {
- callback.call(self, e);
- } else {
- clearTimeout(timer);
+ // Work out the various parameters
+ if ( ! handler) {
+ handler = eventData;
+ } else if ( ! delay && typeof eventData !== 'object') {
+ delay = handler;
+ handler = eventData;
+ }
+
+ // Get delay
+ delay = calcDuration(delay);
+
+ return this.each(function () {
+ var self = this;
+ var timer;
+ var throttle;
+
+ $(self).bind(eventType, function (e) {
+ // We make a note of throttling and only run
+ // handler when not throttling
+ if (name === 'bindThrottle' && ! throttle) {
+ handler.call(self, e);
+ throttle = setTimeout(function () {
+ throttle = null;
+ }, delay);
+ }
+
+ // Every event clears previous timer
+ if (timer) {
+ clearTimeout(timer);
+ }
+
+ // Set a fresh timer
timer = setTimeout(function () {
- timer = null;
- callback.call(self, e);
+ handler.call(self, e);
}, delay);
- }
+ });
});
- });
-
- };
+
+ };
+ });
}(this.jQuery));
Please sign in to comment.
Something went wrong with that request. Please try again.