Skip to content

Commit

Permalink
Better throttling function
Browse files Browse the repository at this point in the history
  • Loading branch information
bseth99 committed Jun 6, 2014
1 parent e1412d8 commit c656233
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery-ui-scrollable",
"version": "0.1.4",
"version": "0.1.5",
"ignore": [
"bower_components",
"node_modules",
Expand Down
47 changes: 34 additions & 13 deletions jquery-ui-scrollable.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,49 @@
* End jQuery UI Position
**/

/**
* throttle from:
* http://remysharp.com/2010/07/21/throttling-function-calls/
*/
function throttle(fn, threshhold, scope) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;

var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
// hold on to it
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
fn.apply(context, args);
}, threshhold);
} else {
last = now;
fn.apply(context, args);
}
};
}

function trackScrolling( scroller ) {

var _waiter = null;

scroller.onscroll = function ( e ) {

if ( !_waiter && !scroller.ignoreScrolling ) {
scroller.onscroll = throttle( function ( e ) {

_waiter = setTimeout(function () {
if ( !scroller.ignoreScrolling ) {

$.each(scroller.watch, function () {
$.each(scroller.watch, function () {

this._checkPositioning( e );
this._checkPositioning( e );

});

_waiter = null;

}, $.osb.scrollable.CONFIG.throttler);
});
}

}
}, $.osb.scrollable.CONFIG.throttler );

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

$.widget('osb.scrollable', {

version: "0.1.4",
version: "0.1.5",

widgetEventPrefix: 'scroll',

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "jquery-ui-scrollable",
"title": "jQuery UI Scrollable",
"description": "Enables monitoring, querying, or changing the scroll position of an element relative to a scrolling container",
"version": "0.1.4",
"version": "0.1.5",
"author": {
"name": "Ben Olson",
"url": "http://bseth99.github.com/"
Expand Down

0 comments on commit c656233

Please sign in to comment.