public
Description:
Homepage:
Clone URL: git://github.com/subtleGradient/subtleheartbeat.git
subtleheartbeat / subtleheartbeat.js
100644 81 lines (65 sloc) 1.917 kb
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
SubtleHeartBeat
Global timing handler
Copyright 2009 Thomas Aylott SubtleGradient.com
MIT License
*/
 
var SubtleHeartBeat = (function(){
function Heart(){
if (this instanceof Heart)
return this.initialize.call(this, Array.prototype.slice.call(arguments));
throw new Error('Must be called with `new`');
};
 
 
// Instances
Heart.prototype = {
 
initialize: function(id, setContext){
this.id = String(id);
if (setContext == null) setContext = true;
if (setContext) Heart.setContext(id, this);
this.reset();
},
 
reset: function(){
this._rhythm = [];
return this;
},
 
beat: function(){
console.log(this._rhythm);
var beater = this._rhythm.shift();
console.log(beater);
if (beater) beater.fn.apply(beater.object, beater.args);
return this;
},
 
delay: function(fn, delay, object, args){
if (typeof fn != 'function')
throw new Error('Argument Error: arguments[0] "fn" must be a function; got '+typeof fn+':"'+String(fn)+'"');
this._rhythm.push({fn:fn, object:object||window, args:args||[], delay:delay||0});
return this;
}
};
 
 
// Generic
Heart.delay = function(fn, delay, object, args){ return Heart._context.delay(fn, delay, object, args); };
Heart.beat = function(){ return Heart._context.beat(); };
 
 
// Contexts
Heart.setContext = function(id, context){
id = String(id || 'default');
Heart._context = Heart._contexts[id] = (context || Heart._contexts[id] || new Heart(id));
return Heart;
};
 
Heart.getContext = function(id){
if (id == null) return Heart._context;
return Heart._contexts[id];
};
 
Heart.reset = function(){
if (Heart._contexts) for (var id in Heart._contexts) {
Heart._contexts[id].reset();
delete Heart._contexts[id];
}
Heart._contexts = {};
delete Heart._context;
Heart.setContext('default');
return Heart;
};
 
 
// Implementation
Heart.reset();
return Heart;
})();