-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
57 lines (43 loc) · 898 Bytes
/
index.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
/**
* Module dependencies
*/
var isArray = Array.isArray
/**
* Export `pulsed`
*/
module.exports = pulsed;
/**
* Returns a function that is executed
* in interval patterns `n` number of times
*
* @api public
* @param {Array} pattern
* @param {Number} count
* @param {Function} fn
*/
function pulsed (pattern, count, fn) {
var to, i = 0, n = 0;
if ('function' === typeof count) {
fn = count;
count = 1;
}
count = pattern.length * count;
if (!isArray(pattern)) {
throw new TypeError("expecting an array");
}
return function f () {
var args = arguments;
var interval = pattern[i++]
if (undefined === interval) {
interval = pattern[(i = 0)];
}
~function run () {
to = setTimeout(function () {
if (n >= count) return;
fn.apply(null, args);
run();
n++;
}, interval);
}();
}
}