DLynch has uploaded a new change for review.
https://gerrit.wikimedia.org/r/275913
Change subject: Add throttle to complement debounce
......................................................................
Add throttle to complement debounce
Change-Id: Ic4bb40d0d6f57420f2582e33636100c2342707b2
---
M src/core.js
1 file changed, 64 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/oojs/ui refs/changes/13/275913/1
diff --git a/src/core.js b/src/core.js
index 59c84f3..37b4aed 100644
--- a/src/core.js
+++ b/src/core.js
@@ -247,6 +247,70 @@
};
/**
+ * Returns a function, that, when invoked, will only be triggered at most once
+ * during a given window of time. Normally, the throttled function will run as
+ * much as it can, without ever going more than once per `wait` duration; but
if
+ * you’d like to disable the execution on the leading edge, pass `{leading:
+ * false}`. To disable execution on the trailing edge, ditto.
+ *
+ * Ported from: http://underscorejs.org/underscore.js
+ *
+ * @param {Function} func
+ * @param {number} wait
+ * @param {Object} [options]
+ * @param {boolean} [options.leading=true] whether to immediately run
+ * @param {boolean} [options.trailing=true] whether to run one last time after
calls end
+ * @return {Function}
+ */
+OO.ui.throttle = function ( func, wait, options ) {
+ var context, args, result,
+ timeout = null,
+ previous = 0,
+ later = function () {
+ previous = options.leading === false ? 0 : OO.ui.now();
+ timeout = null;
+ result = func.apply( context, args );
+ if ( !timeout ) {
+ context = args = null;
+ }
+ };
+ options = options || {};
+ return function () {
+ var remaining,
+ now = OO.ui.now();
+ if ( !previous && options.leading === false ) {
+ previous = now;
+ }
+ remaining = wait - ( now - previous );
+ context = this;
+ args = arguments;
+ if ( remaining <= 0 || remaining > wait ) {
+ if ( timeout ) {
+ clearTimeout( timeout );
+ timeout = null;
+ }
+ previous = now;
+ result = func.apply( context, args );
+ if ( !timeout ) {
+ context = args = null;
+ }
+ } else if ( !timeout && options.trailing !== false ) {
+ timeout = setTimeout( later, remaining );
+ }
+ return result;
+ };
+};
+
+/**
+ * A (possibly faster) way to get the current timestamp as an integer
+ *
+ * @return {number} Current timestamp
+ */
+OO.ui.now = Date.now || function () {
+ return new Date().getTime();
+};
+
+/**
* Proxy for `node.addEventListener( eventName, handler, true )`.
*
* @param {HTMLElement} node
--
To view, visit https://gerrit.wikimedia.org/r/275913
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic4bb40d0d6f57420f2582e33636100c2342707b2
Gerrit-PatchSet: 1
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: DLynch <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits