-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
…ttling
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,70 @@ gxp.util = { | |
return a.href; | ||
}, | ||
|
||
/** api: function[throttle] | ||
* :arg func: ``Function`` | ||
* :arg wait: ``Integer`` | ||
* :return: ``Function`` | ||
* | ||
* Returns a function, that, when invoked, will only be triggered at | ||
* most once during a given window of time. | ||
*/ | ||
throttle: (function() { | ||
|
||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bartvde
via email
Contributor
|
||
// Underscore.js 1.3.3 | ||
// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc. | ||
// Underscore is freely distributable under the MIT license. | ||
// Portions of Underscore are inspired or borrowed from Prototype, | ||
// Oliver Steele's Functional, and John Resig's Micro-Templating. | ||
// For all details and documentation: | ||
// http://documentcloud.github.com/underscore | ||
|
||
// Returns a function, that, as long as it continues to be invoked, will not | ||
// be triggered. The function will be called after it stops being called for | ||
// N milliseconds. If `immediate` is passed, trigger the function on the | ||
// leading edge, instead of the trailing. | ||
var debounce = function(func, wait, immediate) { | ||
var timeout; | ||
return function() { | ||
var context = this, args = arguments; | ||
var later = function() { | ||
timeout = null; | ||
if (!immediate) func.apply(context, args); | ||
}; | ||
if (immediate && !timeout) func.apply(context, args); | ||
clearTimeout(timeout); | ||
timeout = setTimeout(later, wait); | ||
}; | ||
}; | ||
|
||
// Returns a function, that, when invoked, will only be triggered at most once | ||
// during a given window of time. | ||
var throttle = function(func, wait) { | ||
var context, args, timeout, throttling, more, result; | ||
var whenDone = debounce(function(){ more = throttling = false; }, wait); | ||
return function() { | ||
context = this; args = arguments; | ||
var later = function() { | ||
timeout = null; | ||
if (more) func.apply(context, args); | ||
whenDone(); | ||
}; | ||
if (!timeout) timeout = setTimeout(later, wait); | ||
if (throttling) { | ||
more = true; | ||
} else { | ||
result = func.apply(context, args); | ||
} | ||
whenDone(); | ||
throttling = true; | ||
return result; | ||
}; | ||
}; | ||
return function(func, wait) { | ||
return throttle(func, wait); | ||
}; | ||
})(), | ||
|
||
/** api: function[md5] | ||
* :arg data: ``String`` | ||
* :returns: ``String`` md5 hash | ||
|
1 comment
on commit e70a221
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ExtJS already has a 'throttle' concept built into its event listeners and the Ext.util.DelayedTask
class. You could rewrite the throttle function as shown above as a convenience function or you could add a 'timelinescroll' event to the timeline panel and just fire that event as a result of the Simile timeline scroll. Then after the ...addOnScrollListener
call, you could add setPlaybackCenter
as the listener for the 'timelinescroll' event with the buffer
option set to this.scrollInterval
throttling is already implemented in the ExtJs library via the Ext.util.DelayedTask class.
You could replace this with:
throttle: function(func, wait){
var task = new Ext.util.DelayedTask(func);
var f = function(){
task.delay(wait);
};
return f;
}