-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.coffee
45 lines (31 loc) · 895 Bytes
/
timer.coffee
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
timer = (timeouts,interval,cb) ->
if cb is undefined
cb = interval
interval = null
if typeof timeouts is 'number'
timeouts = [ timeouts ]
timeouts_l = timeouts?.length or 0
timeouts_i = 0
entry = clear: -> clearTimer entry
do setup = (call) ->
do cb if call
if timeouts_i < timeouts_l
entry.timeout = setTimeout setup, timeouts[timeouts_i++], true
else if interval?
delete entry.timeout
entry.interval = setInterval cb,interval
return entry
timer.auto = (interval, cb) ->
return timer interval-Date.now()%interval,interval, cb
timer.interval = (interval,cb) ->
return timer null,interval,cb
timer.clear = clearTimer = (entry) ->
if entry?.timeout
clearTimeout entry.timeout
delete entry.timeout
if entry?.interval
clearInterval entry.interval
delete entry.interval
exports?.timer = timer
window?.timer = timer
define? [], -> {timer}