Clumpy.js
- Usage Guide
- - Intro
- - Setup
- - Looping
- - Chaining
- - Loop Control
- - Progress
- - Nesting
- Reference
Reference
This page documents Clumpy, method by method. See the usage guide for more information on how to use these methods together.
Constructor
new Clumpy(options)
Creates a new Clumpy instance.
Parameter Default Description options.betweennullA function to call between clumps. options.delay0The number of milliseconds to rest between clumps. options.duration100The maximum number of milliseconds each clump is allowed to run. Example
// Defaults: var clumpy = new Clumpy(); // With Options Specified: var clumpy = new Clumpy({ between: function () { console.log('Moving right along...'); }, // Devote equal time to processing and resting: delay: 50, duration: 50 });
Looping Actions
The following methods model looping statements.
for_loop(init, test, inc, statements)
Enqueue an action that models a
forloop.
Parameter Description initA function that initializes the loop. testA function that returns the result of the condition for the loop. incA function that increments the loop. statementsA function that performs one iteration of the loop’s body. All parameters are required.
Example
// Real Loop for (i = 0; i < 10; i += 1) { console.log(i); } // Clumpy Equivalent clumpy.for_loop( function () { i = 0; }, function () { return i < 10; }, function () { i += 1; }, function () { console.log(i); } );
for_in_loop(getObject, statements)
Enqueue an action that models a
for...inloop.
Parameter Description getObjectA function that returns the object over which to iterate. statementsA function that performs one iteration of the loop’s body. It should accept an argument representing the key for the current loop iteration. Both parameters are required.
Example
// Real Loop for (key in object) { console.log(key + ': ' + object[key]); } // Clumpy Equivalent clumpy.for_in_loop( function () { return object; }, function (k) { key = k; console.log(key + ': ' + object[key]); } );The scratch variable
kin the example above is for mimicking the scope key has in the real loop. If you don’t need to access the variable from outside the statements function, you can use its argument directly:clumpy.for_in_loop( function () { return object; }, function (key) { console.log(key + ': ' + object[key]); } );
while_loop(test, statements)
Enqueue an action that models a
whileloop.
Parameter Description testA function that returns the result of the condition for the loop. statementsA function that performs one iteration of the loop’s body. Example
// Real Loop while (i < 10) { console.log(i); i += 1; } // Clumpy Equivalent clumpy.while_loop( function () { return i < 10; }, function () { console.log(i); i += 1; } );
do_while_loop(statements, test)
Enqueue an action that models a
do...whileloop.
Parameter Description statementsA function that performs one iteration of the loop’s body. testA function that returns the result of the condition for the loop. Both parameters are required.
Example
// Real Loop do { console.log(i); i += 1; } while (i < 10); // Clumpy Equivalent clumpy.do_while_loop( function () { console.log(i); i += 1; }, function () { return i < 10; } );
Loop Control
The following methods emulate break, continue, and label statements, for modifying the flow of loops.
break_loop(label)
Break the closest loop with the specified label, or the current loop if no label is given.
Parameter Default Description labelundefinedThe label of the loop to break.
continue_loop(label)
Continue the closest loop with the specified label, or the current loop if no label is given.
Parameter Default Description labelundefinedThe label of the loop to continue.
label(name)
Assign a label to the next action enqueued, if the action is a loop. If the next action is not a loop, the label is discarded.
Parameter Description name(required)The string to assign as the label.
Single Actions
The following methods enqueue actions that aren’t loops.
interrupt()
Enqueue an end to the clump that’s running at the time the interrupt is dequeued.
set(options)
Enqueue the act of setting one or more options for this Clumpy instance. Contrast this with
setNowbelow, which sets the options immediately at the time the method is called.The
optionsobject can have the same properties as theClumpyconstructor.
sleep(delay)
Enqueue a sleep of
delaymilliseconds before deueueing the next action.
Parameter Description delay(required)The number of milliseconds to sleep.
then(statements)
Enqueue the
statementsfunction to be called a single time.
Parameter Description statements(required)The function to invoke.
wait(statements)
Enqueue an outside asynchronous operation and wait until that operation completes before dequeueing the next action.
Parameter Description statements(required)The function to call. It should accept a function as an argument ( donein the example below), and invoke that function as a callback when the task is complete.Example
(clumpy .while_loop( // snip ) .wait(function (done) { jQuery.get('/example/async/', function (data) { console.log('Got Data: ' + data); done(); }); }) .for_loop( // snip ) );
External Control
The following methods act immediately instead of enqueueing actions. They are for external control over a Clumpy instance, and should generally not be used inside enqueued actions.
clear()
Stop this Clumpy instance immediately and discard all of its enqueued actions.
pause()
Stops execution immediately until
resumeis called.
resume()
Resumes execution after pause().
setNow(options)
Set options for this Clumpy instance immediately. Contrast this with
setabove, which doesn’t set the options immediately, but enqueues the act of setting the options.The
optionsobject can have the same properties as theClumpyconstructor.