____ __ /\ _`\ /\ \__ __ \ \ \L\ \ __ _____ __ __ \ \ ,_\ /\_\ ____ \ \ , / /'__`\/\ '__`\ /'__`\ /'__`\ \ \ \/ \/\ \ /',__\ \ \ \\ \ /\ __/\ \ \L\ \/\ __//\ \L\.\_\ \ \_ __ \ \ \/\__, `\ \ \_\ \_\ \____\\ \ ,__/\ \____\ \__/.\_\\ \__\/\_\_\ \ \/\____/ \/_/\/ /\/____/ \ \ \/ \/____/\/__/\/_/ \/__/\/_/\ \_\ \/___/ \ \_\ \ \____/ \/_/ \/___/
It abstracts away the need for dealing with setTimeout and setInterval, and the somewhat error prone timer and interval IDs
It provides an simple, intuitive and flexible chained api.
It is ideal for polling, performing asynchronous updates, doing timed UI updates or executing expensive tasks asynchronously .
It does not have any dependencies, but plays nicely with jQuery.
It is built on top of a Deferred/promise implemetation for even greater flexibility
It is tested in IE 7-9, Firefox 10, Chrome 16, Safari 5.1 and Opera 11.61.
jQuery// This is just for creating some example DOM elementsvar ul = jQuery"<ul></ul>" now = getTime elem_count = 100;while elem_count-- ;ulappendjQuery'<li class="live-time" data-timestamp="'+now+Mathrandom*100000*Mathrandom > 0.5 ? -1 : 1 +'"></li>'ulappendTojQuery"body";;var elems = jQuery".live-time";Repeatelemseachelem = jQueryelem;var time = momentelemdata"timestamp";elemhtml'Displaying '+ timeformat"dddd, MMMM Do YYYY, h:mm:ss a" +' as "<b>'+ timefromNow+'</b>"';;every1000 'ms'startnow;;
Polling a server every second, but wait for the request to complete before proceeding
RepeatjQueryajaxurl: ''console.logdata;done; // will wait for this to be called before continuing;every1000 'ms'for2 'minutes'startin3 'secs';
Add a function to the console object, that allows for monitoring a property of any object.
var last_value = objectproperty;return Repeatvar current_value = objectproperty;if last_value !== current_valueconsole.log'Property changed from "'+last_value+'" to "'+current_value+'"!';last_value = current_value;elseconsole.log"No change";;;var myObject = someProperty: "Something";consolemonitormyObject 'someProperty'every500 'ms'for10 'seconds'start;setTimeout myObjectsomeProperty = "Changed value" 3000;
A common trick in order to keep the UI responsive while executing expensive operations is to split up the parts of the operation into smaller subtasks that can be executed asynchronously using timers.
var tasks = openDocument writeText closeDocument updateUI; // these are the expensive functionsreturn Repeatvar task = tasksshift;taskid;asyncuntilreturn taskslength == 0;startnow;
wait() will all return a read-only Promise instance.
Repeat console.log"W00t";every1 's'for20 's'startnowthenconsole.log"I'm done w00t'n";;
The following example illustrates use of success/progress/error listeners:
varconsole.log"All good" results;;varconsole.error"Error" exception;;varconsole.log"Progress: " result;;Repeatvar v = Mathrandom;if v > 0.8 throw "Ouch!";return v;every1 's'for10 's'startthenonSuccess onFailure onProgress;
If used standalone or with jQuery, the promise object also provides more convenient
fail functions for adding callbacks in separate steps.
Additionaly, there's also an
always method to add callback functions for whenever the promise is either resolved or rejected.
The above example using the jQuery or standalone version could be written like this:
var repeat = Repeatvar v = Mathrandom;if v > 0.8 throw "Ouch!";return v;every1 's'for5 's'start;repeatthen console.log'Resolved!'; ;repeatfail console.log'Rejected!'; ;repeatalways console.log'Done! (either resolved or rejected)'; ;
If you are targeting older browsers, you probably want to use any of the ECMAScript 5 polyfill libraries listed here: https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills (in particular ddr-ecma5, augmentjs or es5-shim)
Some older browsers and environments (IE 6 - 8) doesn't support using reserved words as property names (and may throw an error when referencing functions like "for", "while" and "in"), Repeat.js offers alternative function names for code targeted for such environments:
Repeatconsole.log"I'm safe";every2 'sec'lasting2 'min' // lasting instead of forduring return Mathrandom > 5; // during instead of whileprovided return Mathrandom > 5; // provided instead of ifwait5 'sec'; // wait instead of in
Another less readable (and not recommended) workaround would be to reference these methods using bracket notation:
Repeat console.log"Hello";every2 's''for'2 'm''if' return Mathrandom > 5;'while' return Mathrandom > 5;'in'5 's';
Copyright (C) 2012 Bjørge Næss
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.