forked from Touffy/JSSCxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delays.js
75 lines (69 loc) · 1.54 KB
/
delays.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
68
69
70
71
72
73
74
75
/* Delays:
wraps setTimeout and setInterval calls so they can be paused and resumed
*/
function Delay(delay, startNow, sc, proc, e, target, element)
{
this.executed=false
this.time=delay
this.event=e
this.target=target
this.proc=proc
this.sc=sc
this.element=element
if(startNow) this.start()
else this.sc.timeouts.push(this)
}
function Timeout(args, startNow, sc)
{
this.executed=false
this.time=args[1]
this.f=args[0]
this.args=[]
this.sc=sc
// this.element=sc.datamodel._element
for(var i=2; args[i]; i++) this.args.push(args[i])
if(startNow) this.start()
else this.sc.timeouts.push(this)
}
Delay.timesUp=function(t){ t.timesUp() }
Delay.prototype.start=Timeout.prototype.start=function()
{
if(!this.timer && !this.executed)
{
this.timer=setTimeout(Delay.timesUp, this.time, this)
this.started=+new Date()
}
}
Delay.prototype.cancel=Timeout.prototype.cancel=function()
{
if(this.timer && !this.executed){
clearTimeout(this.timer)
delete this.timer
}
}
Delay.prototype.stop=Timeout.prototype.stop=function()
{
if(this.timer && !this.executed && this.sc.running){
this.time-=new Date()-this.started
if(this.time<15) return false
clearTimeout(this.timer)
delete this.timer
this.sc.timeouts.push(this)
return true
}
return false
}
Delay.prototype.timesUp=function()
{
this.executed=true
with(this) proc.send(event, target, element, sc)
}
Timeout.prototype.timesUp=function()
{
this.executed=true
try{
if((typeof this.f)=="function")
sc.datamodel.call(f, this.args)
else sc.datamodel.expr(f, f)
} catch(err){}
}