Chromium Code Reviews| Index: ui/webui/resources/js/util.js |
| diff --git a/ui/webui/resources/js/util.js b/ui/webui/resources/js/util.js |
| index 759bfbbf308ac232374ffec3e12436f68882fbee..64243d678369107ab7ce33336ae14a27eaba0c7a 100644 |
| --- a/ui/webui/resources/js/util.js |
| +++ b/ui/webui/resources/js/util.js |
| @@ -393,6 +393,29 @@ function quoteString(str) { |
| return str.replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, '\\$1'); |
| } |
| +/** |
| + * Calls |callback| and stops listening the first time any event in |eventNames| |
| + * is triggered on |target|. |
| + * @param {!EventTarget} target |
| + * @param {!Array<string>|string} eventNames Array or space-delimited string of |
| + * event names to listen to (e.g. 'click mousedown'). |
| + * @param {function(!Event)} callback Called at most once. The |
| + * optional return value is passed on by the listener. |
| + */ |
| +function listenOnce(target, eventNames, callback) { |
| + if (!Array.isArray(eventNames)) |
| + eventNames = eventNames.split(/ +/); |
| + |
| + var removeAllAndCallCallback = function(event) { |
| + for (var eventName of eventNames) |
|
Dan Beam
2016/09/01 03:02:01
this code runs on iOS, where for..of support may b
michaelpg
2016/09/07 00:30:26
Done.
|
| + target.removeEventListener(eventName, removeAllAndCallCallback, false); |
| + return callback(event); |
| + }; |
| + |
| + for (var eventName of eventNames) |
| + target.addEventListener(eventName, removeAllAndCallCallback, false); |
| +} |
| + |
| // <if expr="is_ios"> |
| // Polyfill 'key' in KeyboardEvent for iOS. |
| // This function is not intended to be complete but should |