GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaScript/Timing-Functions/IntervalTimer.js at master · TheAlgorithms/JavaScript Expand file tree/ Copy pathIntervalTimer.jsMore file actions 92 lines (82 loc) · 2.24 KB / Copy pathIntervalTimer.jsFile metadata and controls92 lines (82 loc) · 2.24 KB 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 /** * @author Nandan V * Sunday, 26 July 2020 8:33 AM * @description Singleton class that handles the <b>timing of tests</b> and * specs. <br/> The class is singleton as <u>javascript does not support * multiple timer instances<u/>. */ class IntervalTimer { /** * @description Constructor for Timer. * @param interval Sets the interval for running the timer. * @param callBack The callback function to be executed. * @return {IntervalTimer} If exists, the existing object. */ constructor(interval = 10, callBack = () => {}) { this.prevInterval = 0 if (this.instance == null) { this.interval = interval this.callBack = callBack this.instance = this } else { return this.instance } } /** * @description Starts the timer. */ startTimer() { this.timer = setInterval(this.callBack, this.interval) } /** * @description Resets the timer. * @return {number} Elapsed time in milliseconds. */ resetTimer() { clearInterval(this.timer) this.callBack = () => {} return this.getElapsedTime() } /** * @return {number} Elapsed time in milliseconds since reset. */ getElapsedTime(offset = 0) { this.timeElapsed = this.timer - this.prevInterval this.prevInterval = this.timer return this.timeElapsed - offset } /** * @return {number} Elapsed time since start. */ getRunTime() { return this.timer } } /** * @author Nandan V * Saturday, 01 August 2020 8:33 AM * @description Example usage */ const ExampleIntervalTimer = function (output = (v) => console.log(v)) { /** * Create am object with default settings. * @type {IntervalTimer} Used to get timing information. */ const timer = new IntervalTimer() timer.startTimer() // ... Initialization code ... // I generally use it for timing tests in Jasmine JS. /** * Gets the runtime till this point. * Can be subtracted from ElapsedTime to offset timing of initialization code. */ const initOffset = timer.getRunTime() // ... A test ... // The time taken to run the test. output(timer.getElapsedTime(initOffset)) /** * Returns the elapsed time and resets the timer to 0. */ output(timer.resetTimer()) } export { IntervalTimer, ExampleIntervalTimer } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |