├── .gitignore
├── test
├── test.js
├── index.html
├── qunit.css
└── qunit.js
├── README
├── demo
└── styles.css
├── bindWithDelay.js
├── onDelay.js
├── index.html
└── onDelay.html
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 |
--------------------------------------------------------------------------------
/test/test.js:
--------------------------------------------------------------------------------
1 | module('onDelay');
2 |
3 | asyncTest("delay", function() {
4 | var count = 0;
5 |
6 | expect(2);
7 |
8 | jQuery(document).onDelay("foo", function() { ++count; }, 50);
9 |
10 | equal(count, 0, "Not incremented yet");
11 |
12 | jQuery(document).trigger("foo").trigger("foo").trigger("foo");
13 |
14 | setTimeout(function() {
15 | equal(count, 1, "Three triggers, one count");
16 | start();
17 | }, 100);
18 | });
--------------------------------------------------------------------------------
/test/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
74 |
bindWithDelay jQuery Plugin (see example)
75 |
The Basics
76 |
77 | - What does bindWithDelay do?
78 | It prevents a function call from happening EVERY time an event is fired from the browser.
79 | Rather than implementing custom timeout code, just add in this plugin and simplify your code.
80 |
81 | - How do I use it in code?
82 |
Pretty much the same as http://api.jquery.com/bind/
83 | element.bindWithDelay( eventType, [ eventData ], handler(eventObject), timeout, throttle )
84 | It works with existing code that uses the bind() function.
85 | Just add an extra parameter (timeout in milliseconds), and an optional boolean value if you would like to enable throttling.
86 | // If your code that uses bind(), just add in a timeout parameter.
87 | $("#element").bind("click", function() {...});
88 | $("#element").bindWithDelay("click", function() {...}, 1000);
89 |
90 | // If your code that uses click(), you will need to do it the same way.
91 | $("#element").click(function() {...}, 1000);
92 | $("#element").bindWithDelay("click", function() {...}, 1000);
93 |
94 | // If you would like to let it still fire once per interval while an event is fired
95 | // (called "throttling"), then put a true in for the throttle parameter:
96 | $("#element").bindWithDelay("click", function() {...}, 1000, true);
97 |
98 |
99 |
100 | - Where is the source code?
101 | http://github.com/bgrins/bindWithDelay/blob/master/bindWithDelay.js
102 |
103 |
104 |
What is the difference between throttling and not?
105 |
Imagine you are scrolling down a long page for 3 seconds and you have bindWithDelay running with a timeout of 1000ms. By default, your event will only fire a second after you have completely finished scrolling. If you have the optional throttling on, it will fire once per second during the scrolling - a total of 3 times instead of 1. Move your mouse around in circles and look at the example below to see:
106 |
107 |
A Simple Example
108 |
As you have been on this page, it has been tracking how many times your browser's mousemove, click,
109 | scroll, and resize events have fired (both with and without bindWithDelay).
110 | Go ahead and move your mouse around and click (try clicking a bunch of times in a row to see what it does)
111 | , or resize your browser to get a look at how it works.
112 |
113 |
114 |
$(document).bind("mousemove") has been called: 0 times.
115 |
$(document).bindWithDelay("mousemove", 1000) has been called: 0 times.
116 |
$(document).bindWithDelay("mousemove", 1000, true) has been called: 0 times.
117 |
118 |
119 |
$(document).bind("click") has been called: 0 times.
120 |
$(document).bindWithDelay("click", 1000) has been called: 0 times.
121 |
$(document).bindWithDelay("click", 1000, true) has been called: 0 times.
122 |
123 |
124 |
$(document).bind("scroll") has been called: 0 times.
125 |
$(document).bindWithDelay("scroll", 1000) has been called: 0 times.
126 |
$(document).bindWithDelay("scroll", 1000, true) has been called: 0 times.
127 |
128 |
129 |
$(window).bind("resize") has been called: 0 times.
130 |
$(window).bindWithDelay("resize", 1000) has been called: 0 times.
131 |
$(window).bindWithDelay("resize", 1000, true) has been called: 0 times.
132 |
133 |
134 |
135 |
136 |
139 |
140 |
--------------------------------------------------------------------------------
/onDelay.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
74 |
bindWithDelay jQuery Plugin (see example)
75 |
The Basics
76 |
77 | - What does bindWithDelay do?
78 | It prevents a function call from happening EVERY time an event is fired from the browser.
79 | Rather than implementing custom timeout code, just add in this plugin and simplify your code.
80 |
81 | - How do I use it in code?
82 |
Pretty much the same as http://api.jquery.com/bind/
83 | element.bindWithDelay( eventType, [ eventData ], handler(eventObject), timeout, throttle )
84 | It works with existing code that uses the bind() function.
85 | Just add an extra parameter (timeout in milliseconds), and an optional boolean value if you would like to enable throttling.
86 | // If your code that uses bind(), just add in a timeout parameter.
87 | $("#element").bind("click", function() {...});
88 | $("#element").bindWithDelay("click", function() {...}, 1000);
89 |
90 | // If your code that uses click(), you will need to do it the same way.
91 | $("#element").click(function() {...}, 1000);
92 | $("#element").bindWithDelay("click", function() {...}, 1000);
93 |
94 | // If you would like to let it still fire once per interval while an event is fired
95 | // (called "throttling"), then put a true in for the throttle parameter:
96 | $("#element").bindWithDelay("click", function() {...}, 1000, true);
97 |
98 |
99 |
100 | - Where is the source code?
101 | http://github.com/bgrins/bindWithDelay/blob/master/bindWithDelay.js
102 |
103 |
104 |
What is the difference between throttling and not?
105 |
Imagine you are scrolling down a long page for 3 seconds and you have bindWithDelay running with a timeout of 1000ms. By default, your event will only fire a second after you have completely finished scrolling. If you have the optional throttling on, it will fire once per second during the scrolling - a total of 3 times instead of 1. Move your mouse around in circles and look at the example below to see:
106 |
107 |
A Simple Example
108 |
As you have been on this page, it has been tracking how many times your browser's mousemove, click,
109 | scroll, and resize events have fired (both with and without bindWithDelay).
110 | Go ahead and move your mouse around and click (try clicking a bunch of times in a row to see what it does)
111 | , or resize your browser to get a look at how it works.
112 |
113 |
114 |
$(document).on("mousemove") has been called: 0 times.
115 |
$(document).onDelay("mousemove", 1000) has been called: 0 times.
116 |
$(document).onDelay("mousemove", 1000, true) has been called: 0 times.
117 |
118 |
119 |
$(document).on("click") has been called: 0 times.
120 |
$(document).onDelay("click", 1000) has been called: 0 times.
121 |
$(document).onDelay("click", 1000, true) has been called: 0 times.
122 |
123 |
124 |
$(document).on("scroll") has been called: 0 times.
125 |
$(document).onDelay("scroll", 1000) has been called: 0 times.
126 |
$(document).onDelay("scroll", 1000, true) has been called: 0 times.
127 |
128 |
129 |
$(window).on("resize") has been called: 0 times.
130 |
$(window).onDelay("resize", 1000) has been called: 0 times.
131 |
$(window).onDelay("resize", 1000, true) has been called: 0 times.
132 |
133 |
134 |
135 |
136 |
139 |
140 |
--------------------------------------------------------------------------------
/test/qunit.js:
--------------------------------------------------------------------------------
1 | /**
2 | * QUnit v1.11.0 - A JavaScript Unit Testing Framework
3 | *
4 | * http://qunitjs.com
5 | *
6 | * Copyright 2012 jQuery Foundation and other contributors
7 | * Released under the MIT license.
8 | * http://jquery.org/license
9 | */
10 |
11 | (function( window ) {
12 |
13 | var QUnit,
14 | assert,
15 | config,
16 | onErrorFnPrev,
17 | testId = 0,
18 | fileName = (sourceFromStacktrace( 0 ) || "" ).replace(/(:\d+)+\)?/, "").replace(/.+\//, ""),
19 | toString = Object.prototype.toString,
20 | hasOwn = Object.prototype.hasOwnProperty,
21 | // Keep a local reference to Date (GH-283)
22 | Date = window.Date,
23 | defined = {
24 | setTimeout: typeof window.setTimeout !== "undefined",
25 | sessionStorage: (function() {
26 | var x = "qunit-test-string";
27 | try {
28 | sessionStorage.setItem( x, x );
29 | sessionStorage.removeItem( x );
30 | return true;
31 | } catch( e ) {
32 | return false;
33 | }
34 | }())
35 | },
36 | /**
37 | * Provides a normalized error string, correcting an issue
38 | * with IE 7 (and prior) where Error.prototype.toString is
39 | * not properly implemented
40 | *
41 | * Based on http://es5.github.com/#x15.11.4.4
42 | *
43 | * @param {String|Error} error
44 | * @return {String} error message
45 | */
46 | errorString = function( error ) {
47 | var name, message,
48 | errorString = error.toString();
49 | if ( errorString.substring( 0, 7 ) === "[object" ) {
50 | name = error.name ? error.name.toString() : "Error";
51 | message = error.message ? error.message.toString() : "";
52 | if ( name && message ) {
53 | return name + ": " + message;
54 | } else if ( name ) {
55 | return name;
56 | } else if ( message ) {
57 | return message;
58 | } else {
59 | return "Error";
60 | }
61 | } else {
62 | return errorString;
63 | }
64 | },
65 | /**
66 | * Makes a clone of an object using only Array or Object as base,
67 | * and copies over the own enumerable properties.
68 | *
69 | * @param {Object} obj
70 | * @return {Object} New object with only the own properties (recursively).
71 | */
72 | objectValues = function( obj ) {
73 | // Grunt 0.3.x uses an older version of jshint that still has jshint/jshint#392.
74 | /*jshint newcap: false */
75 | var key, val,
76 | vals = QUnit.is( "array", obj ) ? [] : {};
77 | for ( key in obj ) {
78 | if ( hasOwn.call( obj, key ) ) {
79 | val = obj[key];
80 | vals[key] = val === Object(val) ? objectValues(val) : val;
81 | }
82 | }
83 | return vals;
84 | };
85 |
86 | function Test( settings ) {
87 | extend( this, settings );
88 | this.assertions = [];
89 | this.testNumber = ++Test.count;
90 | }
91 |
92 | Test.count = 0;
93 |
94 | Test.prototype = {
95 | init: function() {
96 | var a, b, li,
97 | tests = id( "qunit-tests" );
98 |
99 | if ( tests ) {
100 | b = document.createElement( "strong" );
101 | b.innerHTML = this.nameHtml;
102 |
103 | // `a` initialized at top of scope
104 | a = document.createElement( "a" );
105 | a.innerHTML = "Rerun";
106 | a.href = QUnit.url({ testNumber: this.testNumber });
107 |
108 | li = document.createElement( "li" );
109 | li.appendChild( b );
110 | li.appendChild( a );
111 | li.className = "running";
112 | li.id = this.id = "qunit-test-output" + testId++;
113 |
114 | tests.appendChild( li );
115 | }
116 | },
117 | setup: function() {
118 | if ( this.module !== config.previousModule ) {
119 | if ( config.previousModule ) {
120 | runLoggingCallbacks( "moduleDone", QUnit, {
121 | name: config.previousModule,
122 | failed: config.moduleStats.bad,
123 | passed: config.moduleStats.all - config.moduleStats.bad,
124 | total: config.moduleStats.all
125 | });
126 | }
127 | config.previousModule = this.module;
128 | config.moduleStats = { all: 0, bad: 0 };
129 | runLoggingCallbacks( "moduleStart", QUnit, {
130 | name: this.module
131 | });
132 | } else if ( config.autorun ) {
133 | runLoggingCallbacks( "moduleStart", QUnit, {
134 | name: this.module
135 | });
136 | }
137 |
138 | config.current = this;
139 |
140 | this.testEnvironment = extend({
141 | setup: function() {},
142 | teardown: function() {}
143 | }, this.moduleTestEnvironment );
144 |
145 | this.started = +new Date();
146 | runLoggingCallbacks( "testStart", QUnit, {
147 | name: this.testName,
148 | module: this.module
149 | });
150 |
151 | // allow utility functions to access the current test environment
152 | // TODO why??
153 | QUnit.current_testEnvironment = this.testEnvironment;
154 |
155 | if ( !config.pollution ) {
156 | saveGlobal();
157 | }
158 | if ( config.notrycatch ) {
159 | this.testEnvironment.setup.call( this.testEnvironment );
160 | return;
161 | }
162 | try {
163 | this.testEnvironment.setup.call( this.testEnvironment );
164 | } catch( e ) {
165 | QUnit.pushFailure( "Setup failed on " + this.testName + ": " + ( e.message || e ), extractStacktrace( e, 1 ) );
166 | }
167 | },
168 | run: function() {
169 | config.current = this;
170 |
171 | var running = id( "qunit-testresult" );
172 |
173 | if ( running ) {
174 | running.innerHTML = "Running: