Skip to content

Instantly share code, notes, and snippets.

@edyrkaj
Last active February 5, 2020 15:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edyrkaj/462d2f707dd60db7253dc58db62628f6 to your computer and use it in GitHub Desktop.
Save edyrkaj/462d2f707dd60db7253dc58db62628f6 to your computer and use it in GitHub Desktop.
Helper method for countdown
/**
* This class has a static a method `run` which needs to pass a callback method in order to get back the values
* for minutes and seconds
* If you need to show number values with zeros on pad start you have an example at the end of this code:
* Countdown
* run {method}
* @author Eledi Dyrkaj <edyrkaj@gmail.com>
*/
export class Countdown {
/**
* @param callback(response) callback function passed as parameter to return values to parent object
* @countdownMinutues| optional minutes to wait for this countdown
*/
static run(calllback: (_timer) => void, countdownMinutes: number = 30) {
const second = 1000,
minute = second * 60,
hour = minute * 60;
const nowDate = new Date(),
countDown = new Date(new Date().setMinutes(nowDate.getMinutes() + countdownMinutes)).getTime(),
x = setInterval(() => {
const now = new Date().getTime(),
distance = countDown - now;
calllback({
minutes: Math.floor((distance % (hour)) / (minute)),
seconds: Math.floor((distance % (minute)) / (second))
});
// close countdown
if (distance <= 1) { clearInterval(x); }
}, second);
}
}
/*
const timer = { minutes: '30', seconds: '00' };
Countdown.run((timer) => {
this.timer.minutes = String(timer.minutes).padStart(2, '0');
this.timer.seconds = String(timer.seconds).padStart(2, '0');
console.log(timer); // 29:59 ... 29:09
}, 30);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment