Timer //JavaScript Repository

Description

Chronometers the amount of miliseconds gone from an instant to another.
Created: 2005.11.22

Code (Download)

//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/classes/timer [rev. #1]

Timer = function(){
    return new Date().getTime();
};
Timer.prototype.start = function(){
    return this.init = Timer();
};
Timer.prototype.stop = function(){
    return (Timer() - this.init);
};

Example (Example)

<script type="text/javascript">
//<![CDATA[

var t = new Timer;
document.write("Iniciou: ", t.start(), "<br />");
for(var i, a = new Array(i = 3000); i; a[--i] = 0);
document.write("Finalizou em: ", t.stop(), "msecs");

//]]>
</script>

Help

Constructor

Timer(void)
Generates an instance of Timer.

Methods

Timer.start(void): Integer
Starts the chronometer and returns the number of miliseconds since 1970-01-01.
Timer.stop(void): Integer
Stops the chronometer cronometragem and returns the amount of mileseconds that passed since "start" was called.

Rank (Votes: 12)

3.25