package.src.utils.Timer.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sweetalert2 Show documentation
Show all versions of sweetalert2 Show documentation
A beautiful, responsive, customizable and accessible (WAI-ARIA) replacement for JavaScript's popup boxes, supported fork of sweetalert
The newest version!
export default class Timer {
/**
* @param {Function} callback
* @param {number} delay
*/
constructor(callback, delay) {
this.callback = callback
this.remaining = delay
this.running = false
this.start()
}
/**
* @returns {number}
*/
start() {
if (!this.running) {
this.running = true
this.started = new Date()
this.id = setTimeout(this.callback, this.remaining)
}
return this.remaining
}
/**
* @returns {number}
*/
stop() {
if (this.started && this.running) {
this.running = false
clearTimeout(this.id)
this.remaining -= new Date().getTime() - this.started.getTime()
}
return this.remaining
}
/**
* @param {number} n
* @returns {number}
*/
increase(n) {
const running = this.running
if (running) {
this.stop()
}
this.remaining += n
if (running) {
this.start()
}
return this.remaining
}
/**
* @returns {number}
*/
getTimerLeft() {
if (this.running) {
this.stop()
this.start()
}
return this.remaining
}
/**
* @returns {boolean}
*/
isRunning() {
return this.running
}
}