Skip to content

Commit c656233

Browse files
committed
Better throttling function
1 parent e1412d8 commit c656233

3 files changed

Lines changed: 36 additions & 15 deletions

File tree

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jquery-ui-scrollable",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"ignore": [
55
"bower_components",
66
"node_modules",

jquery-ui-scrollable.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,49 @@
6565
* End jQuery UI Position
6666
**/
6767

68+
/**
69+
* throttle from:
70+
* http://remysharp.com/2010/07/21/throttling-function-calls/
71+
*/
72+
function throttle(fn, threshhold, scope) {
73+
threshhold || (threshhold = 250);
74+
var last,
75+
deferTimer;
76+
return function () {
77+
var context = scope || this;
78+
79+
var now = +new Date,
80+
args = arguments;
81+
if (last && now < last + threshhold) {
82+
// hold on to it
83+
clearTimeout(deferTimer);
84+
deferTimer = setTimeout(function () {
85+
last = now;
86+
fn.apply(context, args);
87+
}, threshhold);
88+
} else {
89+
last = now;
90+
fn.apply(context, args);
91+
}
92+
};
93+
}
94+
6895
function trackScrolling( scroller ) {
6996

7097
var _waiter = null;
7198

72-
scroller.onscroll = function ( e ) {
73-
74-
if ( !_waiter && !scroller.ignoreScrolling ) {
99+
scroller.onscroll = throttle( function ( e ) {
75100

76-
_waiter = setTimeout(function () {
101+
if ( !scroller.ignoreScrolling ) {
77102

78-
$.each(scroller.watch, function () {
103+
$.each(scroller.watch, function () {
79104

80-
this._checkPositioning( e );
105+
this._checkPositioning( e );
81106

82-
});
83-
84-
_waiter = null;
85-
86-
}, $.osb.scrollable.CONFIG.throttler);
107+
});
87108
}
88109

89-
}
110+
}, $.osb.scrollable.CONFIG.throttler );
90111

91112
//console.log( 'Start listening to scrolling' );
92113
scroller.element.on('scroll.scrollable', scroller.onscroll);
@@ -163,7 +184,7 @@ window.Scrollable = monitor;
163184

164185
$.widget('osb.scrollable', {
165186

166-
version: "0.1.4",
187+
version: "0.1.5",
167188

168189
widgetEventPrefix: 'scroll',
169190

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "jquery-ui-scrollable",
33
"title": "jQuery UI Scrollable",
44
"description": "Enables monitoring, querying, or changing the scroll position of an element relative to a scrolling container",
5-
"version": "0.1.4",
5+
"version": "0.1.5",
66
"author": {
77
"name": "Ben Olson",
88
"url": "http://bseth99.github.com/"

0 commit comments

Comments
 (0)