Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

Timer.as

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
/* 
Timer class, uses getTimer for timing
timerInstance = new Timer( [ startCountDown ] );
- constructor, creates timer instance.  Optional startCountDown to start timer

* Methods:
timerInstance.start();
- begins timer, if the timer is already playing (not paused) start will restart it

timerInstance.stop();
- pauses timer

timerInstance.reset( [ startCountDown ] );
- resets timer time to 0


* Properties:
timerInstance.paused;
- boolean; determines whether timer is playing (true) or not (false)

timerInstance.time;
- number; time in milliseconds the timer has been running
*/


Timer = function(start){
	this.reset(start);
}
Timer.prototype.start = function(){
	if (this._pauseTime){
		this._startTime += getTimer() - this._pauseTime;
		this._pauseTime = 0;
	}else{
		this.reset(true);
	}
}
Timer.prototype.stop = function(){
	if (!this._pauseTime) this._pauseTime = getTimer();
}
Timer.prototype.reset = function(restart){
	this._startTime = getTimer();
	if (!this._startTime) this._startTime = 1;
	if (restart) this._pauseTime = 0;
	else this._pauseTime = this._startTime;
}

Timer.prototype.getTime = function(){
	if (this._pauseTime) return this._pauseTime - this._startTime;
	var gotTime = getTimer();
	if (!gotTime) gotTime = 1;
	return gotTime - this._startTime;
}
Timer.prototype.setTime = function(t){
	this._startTime = getTimer() - t;
}
Timer.prototype.addProperty("time",Timer.prototype.getTime,Timer.prototype.setTime);

Timer.prototype.getPaused = function(){
	return (this._pauseTime) ? true : false;
}
Timer.prototype.setPaused = function(p){
	if (p == Boolean(this._pauseTime)) return false;
	if (p) this.stop();
	else this.start();
}
Timer.prototype.addProperty("paused",Timer.prototype.getPaused,Timer.prototype.setPaused);