/////////////////////////////////////////////////////////////////////////////// // Object: Timer // Usage: Time how long things take or delay script execution // Input: // Return: Timer object // Example: // // var a = new Timer(); // for (var i = 0; i < 2; i++) // a.pause(3.33333); // a.getElapsed(); // /////////////////////////////////////////////////////////////////////////////// function Timer() { // member properties this.startTime = new Date(); this.endTime = new Date(); // member methods // reset the start time to now this.start = function () { this.startTime = new Date(); } // reset the end time to now this.stop = function () { this.endTime = new Date(); } // get the difference in milliseconds between start and stop this.getTime = function () { return (this.endTime.getTime() - this.startTime.getTime()) / 1000; } // get the current elapsed time from start to now, this sets the endTime this.getElapsed = function () { this.endTime = new Date(); return this.getTime(); } // pause for this many seconds this.pause = function ( inSeconds ) { var t = 0; var s = new Date(); while( t < inSeconds ) { t = (new Date().getTime() - s.getTime()) / 1000; } } }