console
class ZephyrCountdown {
constructor(targetDate) {
this.targetTime = new Date(targetDate).getTime()
this.intervalId = null
}
makeCountdown(res = Function) {
const currTime = new Date().getTime()
const leftTime = this.targetTime - currTime
if (leftTime <= 0) clearInterval(this.intervalId)
const d = Math.floor(leftTime / (1000 * 60 * 60 * 24))
const h = Math.floor((leftTime / (1000 * 60 * 60)) % 24)
const m = Math.floor((leftTime / (1000 * 60)) % 60);
const s = Math.floor((leftTime / 1000) % 60);
const left = {d, h, m, s}
res(left)
}
start(val = Function) {
this.intervalId = setInterval(() => {
this.makeCountdown( res => val(res))
}, 1000)
}
stop() {
clearInterval(this.intervalId)
}
}
const counter = new ZephyrCountdown('2023-05-20 12:26:59')
counter.start( val => {
console.log('val: ', val)
})
<h1 id="text"></h1>