Bret Comnes 80459935e1
0.0.1
2020-02-12 16:58:30 -07:00

35 lines
584 B
JavaScript

/**
* Simple timer lets you record start and stop times, with an elapsed time getter.
*/
class SimpleTimer {
constructor (startTime) {
this.start = startTime || Date.now()
this.end = null
this.stopped = false
}
get elapsed () {
if (this.stopped) {
return this.end - this.start
} else {
return Date.now() - this.start
}
}
stop () {
if (this.stopped) return
this.stopped = true
this.end = Date.now()
}
toString () {
return this.elapsed
}
toJSON () {
return this.elapsed
}
}
module.exports = SimpleTimer