Created
February 13, 2014 19:03
-
-
Save carymrobbins/8981590 to your computer and use it in GitHub Desktop.
JavaScript until function - an improved setInterval.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> | |
| <script type="text/javascript"> | |
| /** | |
| * A better version of setInterval. Usage: | |
| * until([ | |
| * { action: function() { $('#foo').length > 1; }, | |
| * cleanup: function() { $('#foo').remove(); }, | |
| * interval: 1000 } | |
| * ]); | |
| * | |
| * @param args - Single option or array of options below to execute | |
| * in sequence after each action() and, optionally, cleanup() | |
| * function is called. | |
| * Options: | |
| * action - Call until action() returns true. | |
| * cleanup - Call once action() has returned true. | |
| * interval - Milliseconds between calls to action(). Default 500. | |
| */ | |
| function until(args) { | |
| "use strict"; | |
| // Coerce args to array. | |
| if (typeof args.length === 'undefined') { | |
| args = [args]; | |
| } | |
| var i = 0, | |
| len = args.length, | |
| interval = defaultFor(args[i].interval, 500), | |
| timer, | |
| callback = function() { | |
| var options = args[i], | |
| action = defaultFor(options.action, function() { return true; }), | |
| cleanup = defaultFor(options.cleanup, identity), | |
| interval = defaultFor(options.interval, 500), | |
| result = action(); | |
| if (result) { | |
| clearInterval(timer); | |
| cleanup(); | |
| if (++i < len) { | |
| timer = setInterval(callback, interval); | |
| } | |
| } | |
| }; | |
| timer = setInterval(callback, interval); | |
| } | |
| function identity(x) { | |
| "use strict"; | |
| return x; | |
| } | |
| /** | |
| * @param {*} arg | |
| * @param {*} default_value | |
| * Returns default_value if arg is undefined, otherwise returns arg | |
| */ | |
| function defaultFor(arg, default_value) { | |
| "use strict"; | |
| return arg === undefined ? default_value : arg; | |
| } | |
| // Example usage: | |
| $(document).ready(function() { | |
| var button = $('#button'), | |
| output = $('#output'); | |
| button.on('click', function(e) { | |
| output.text('clicked'); | |
| }); | |
| until([{ | |
| action: function() { return output.text() == 'clicked'; }, | |
| cleanup: function() { output.text('cleanup'); }, | |
| }, { | |
| action: function() { return output.text() == 'clicked'; }, | |
| cleanup: function() { output.text('cleanup again'); }, | |
| interval: 1000 | |
| }]) | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <input id="button" type="button" value="click me"/> | |
| <p id="output"></p> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment