Create a count down timer with a count down listener every second.
createCountdown(inputOptions[,callbackOptions])
inputOptions
<Object>
Specify the time limit for the count downh
<Number>
Time limit in hoursm
<Number>
Time limit in minutess
<Number>
Time limit in seconds
callbackOptions
<Object>
listen
<Function
> Listens every second of the count downcountdown
<Object>
Remaining time in the count downh
<Number>
Remaining time in hourshh
<String>
Remaining time in two digit hours (Single digit hours will be preceeded by zero)m
<Number>
Remaining time in minutesmm
<String>
Remaining time in two digit minutes (Single digit minutes will be preceeded by zero)s
<Number>
Remaining time in secondsss
<String>
Remaining time in two digit seconds (Single digit seconds will be preceeded by zero)
done
<Function>
Called when the count down completes
- Returns:
<Object>
start
<Function>
Starts the count down on timerstop
<Function>
Stops the count down on the timerreset
<Function>
Resets the count down on the timerset
<Function>
Sets the count down on the timer
This module exports a single function that can used to set a timer, start the timer and listen to the remaining time in the count down every second. The listener is helpful in displaying the remaining time in any format that is desired. The remaining time is always split between hours, minutes and seconds. It doesn't have the consolidated time in seconds or minutes.
- Create a timer for 90 minutes
const timer = createCountdown({
h: 1,
m: 30,
s: 0
}, {
listen: ({hh, mm, ss}) => {
console.log(`${hh}:${mm}:${ss}`)
}
});
timer.start();
/*
01:30:00
01:29:59
...
*/
or
const timer = createCountdown({
h: 0,
m: 90,
s: 0
}, {
listen: ({h, mm, ss}) => {
console.log(`${h}:${mm}:${ss}`)
}
});
timer.start();
/*
1:30:00
1:29:59
...
*/
- Reset the timer
const timer = createCountdown({
h: 1,
m: 30,
s: 0
}, {
listen: ({hh, mm, ss}) => {
console.log(`${hh}:${mm}:${ss}`)
}
});
timer.start();
setTimeout(() => {
timer.reset();
}, 1000);
/*
01:30:00
01:29:59
01:30:00
*/
- Stop the timer
const timer = createCountdown({
h: 1,
m: 30,
s: 0
}, {
listen: ({hh, mm, ss}) => {
console.log(`${hh}:${mm}:${ss}`)
}
});
timer.start();
setTimeout(() => {
timer.stop();
}, 1000);
/*
01:30:00
01:29:59
*/
- Set a timer for 30 seconds
const timer = createCountdown({}, {
listen: ({hh, mm, ss}) => {
console.log(`${hh}:${mm}:${ss}`)
}
});
timer.set({
h: 0,
m: 0,
s: 30,
});
timer.start();
/*
00:00:30
00:00:29
...
*/