Skip to content

Instantly share code, notes, and snippets.

@aasumitro
Last active December 23, 2021 05:52
Show Gist options
  • Save aasumitro/6a4f26125e5b78542330b455f518c820 to your computer and use it in GitHub Desktop.
Save aasumitro/6a4f26125e5b78542330b455f518c820 to your computer and use it in GitHub Desktop.
Counting Down Time
let counting
const SECONDS = 'SECONDS'
const MINUTES = 'MINUTES'
// this function will countdown the given time
//
// param time (number)
// param type (MINUTES, SECONDS)
//
// usage countdown(10, SECONDS) or countdown(1, MINUTES)
function countdown(time, type) {
let refreshTime
if (type === MINUTES) {
refreshTime = parseInt(time) * 60 * 1000
}
if (type === SECONDS) {
refreshTime = parseInt(time) * 1000
}
counting = setInterval(() => {
refreshTime -= 1000
let seconds = Math.floor((refreshTime / 1000) % 60)
let minutes = Math.floor((refreshTime / (1000 * 60)) % 60)
seconds = (seconds < 10) ? "0" + seconds : seconds
minutes = (minutes < 10) ? "0" + minutes : minutes
console.log(`counting down : ${minutes}:${seconds}`)
if (seconds === '00') {
console.log("clear countdown")
clearInterval(counting)
}
}, 1000) // interval running every-second
}
countdown(20, SECONDS)
let counting
const SECONDS = 'SECONDS'
const MINUTES = 'MINUTE'
const SHOULD_NOTIFY = ['00:27', '00:20', '00:10']
// this function will countdown the given time
//
// param time in number
// param type (MINUTES, SECONDS)
//
// usage countdown(10, SECONDS) or countdown(60, MINUTES)
function countdown(time, type) {
let refreshTime
if (type === MINUTES) {
refreshTime = parseInt(time) * 60 * 1000
}
if (type === SECONDS) {
refreshTime = parseInt(time) * 1000
}
counting = setInterval(() => {
refreshTime -= 1000
let seconds = Math.floor((refreshTime / 1000) % 60)
let minutes = Math.floor((refreshTime / (1000 * 60)) % 60)
seconds = (seconds < 10) ? "0" + seconds : seconds
minutes = (minutes < 10) ? "0" + minutes : minutes
concatenate = `${minutes}:${seconds}`
notify(concatenate)
console.log(`counting down : ${concatenate}`)
if (seconds === '00') {
console.log("clear countdown")
clearInterval(counting)
}
}, 1000) // interval running every-second
}
function notify(currentTime) {
if (SHOULD_NOTIFY.includes(currentTime)) {
console.log(`Please notify at: ${currentTime}`)
}
}
countdown(30, SECONDS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment