Needed a timer for a game and then figured I could create a general timer that would also do countdowns and other stuff.
Version information:
/*
Made by fry at friedcellcollective dot net
Use and distribute this freely. Just include this comment block.
Version 1.0
new Timer:public
usage
new Timer(global,[split1, split2,...,splitn])
parameters
global If set to true the time is retrieved from Date object, if false from getTimer()
split1, split2,...,splitn Optional parameters that define how the time is split (default is 1000,60,60,24)
returns
A reference to the newly created Timer object
description
Constructor; creates a new Timer object
example
myTimerObject = new Timer();
myTimerObject = new Timer(1000); // split only into milliseconds and seconds
Timer._getTime:private
Timer.setStart:public
usage
myTimerObject.setStart(startTime)
parameters
startTime Start time in milliseconds
returns
A millisecond value defining the start time of the timer (derived from getTimer() if global is false and from Date object if global is true).
description
Method; sets the start time of the timer to the specified value.
example
myTimerObject = new Timer(true); // global
myTimerObject.setStart(new Date(2004,0,0)); // start time is set to the start of year 2004
myTimerObject = new Timer();
myTimerObject.setStart(1000); // start time is set to one second after the flash player started
Timer.setSplit:public
usage
myTimerObject.setSplit([split1, split2,...,splitn])
parameters
split1, split2,...,splitn Optional parameters that define how the time is split. If not specified no split will be made (returned time will be in milliseconds).
returns
An array of the time passed from the start of the timer split by the new split array.
description
Method; sets the array that defines how the passed time is split.
example
myTimerObject = new Timer(1000,60);
equals
myTimerObject = new Timer();
myTimerObject.setSplit(1000,60);
Timer.restart:public
usage
myTimerObject.restart()
parameters
none.
returns
A millisecond value defining the start time of the timer.
description
Action; restarts the timer.
example
myTimerObject = new Timer();
...
myTimerObject.restart();
Timer.getSplit:public
usage
myTimerObject.getSplit()
parameters
none.
returns
An array of the time passed from the start of the timer split by the internal split array.
description
Method; calculates the time spent between now and the start of the timer and splits it.
example
myTimerObject = new Timer();
...
splitTime = myTimerObject.getSplit();
Math.split:public
usage
Math.split(number, splitArray)
parameters
number The number to split
splitArray The array to split it by
returns
An array of residuals of division by splitArray
description
Method; divides the number by every number in the splitArray and puts the residuals in an array. At the end the remaining number is added to the end of the array.
example
Math.split(15317,[1000]);
// returns [317,15]
*/
_global.Timer = function(global) {
this.global = global;
this.startTime = this._getTime();
this.splitInto = (arguments.length<=1)? [1000,60,60,24] : arguments.splice(0,1);
}
Timer.prototype._getTime = function() {
if (this.global) {
var d = new Date();
return d.valueOf();
} else return getTimer();
}
Timer.prototype.setStart = function(startTime) {
this.startTime = startTime;
return this.startTime;
}
Timer.prototype.setSplit = function() {
this.splitInto = arguments;
return this.getSplit();
}
Timer.prototype.restart = function() {
return this.setStart(this._getTime());
}
Timer.prototype.getSplit = function() {
var splitInto = (arguments.length==0)? this.splitInto : arguments;
return Math.split(this._getTime()-this.startTime,splitInto);
}
ASSetPropFlags(_global,"Timer",3);
ASSetPropFlags(_global.Timer.prototype,"_getTime,setStart,setSplit,restart,getSplit",3);
Math.split = function(num, splitArray) {
var r = new Array();
for (var i=0;i<splitArray.length;i++) {
r[i] = num%splitArray[i];
num = int(num/splitArray[i]);
}
r.push(num);
return r;
}
ASSetPropFlags(Math,"split",3);