-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChronoJs-BabySteps.js
executable file
·67 lines (57 loc) · 1.41 KB
/
ChronoJs-BabySteps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* LIBRARY: ChronoJS
*
* Author: Pablo E. Benito - pelicanorojo - bioingbenito@gmail.com
* https://github.com/pelicanorojo/ChronoJS
*
*/
/**
* MODULE: BabySteps
*
* You can use CRNJS.babySteps for create a sequence in wich you add functions to ron in a specified delay relative to the previous one.
*
*/
(function ( CRNJS ) {
CRNJS.babySteps = function ( generalContext ) {
var
seq = [],
pub = {},
repetitions = 0,
nextInd = 0;
function addNext ( fn, dT, context, args ) {
seq.push({
fn: fn,
dT: dT || 0,
args: Object.prototype.toString.call( args ) === '[object Array]' ? args : [ args ],
context: context || generalContext
});
return pub;
}
function executeNext () {
var
next = seq[ nextInd++ ];
if ( next ) {
window.setTimeout ( function () {
if ( typeof next.fn === 'function' ) {
next.fn.apply ( next.context, next.args );
}
executeNext();
}, next.dT );
} else if ( repetitions > 0 ) {
repetitions--;
nextInd = 0;
executeNext();
}
return pub;
}
function repeat ( aRepetitions ) {
repetitions = aRepetitions ? aRepetitions : ( Number.MAX_VALUE ) * 2;
return pub;
}
pub.addNext = addNext;
pub.start = executeNext;
pub.repeat = repeat;
pub.wait = function ( dT ) { return addNext( null, dT ); };
return pub;
};
}( CRNJS ));