cntdwn.js v1.0.0-rc-1
Cntdwn
A simple to use countdown library
Installation
npm install cntdwn.js
API
Methods
- start()
- cancel()
- pause()
- resume()
- getTimeleft()
- getTimeFrom()
- getTimeTo()
- isPaused()
- isRunning()
- setTimeFrom()
- setTimeTo()
- setOnTick()
- setOnStart()
- setOnCancel()
- setOnFinish()
- setOnPause()
- setOnResume()
start()
Description
Sets up the new countdown.
Parameters
None
Returns
The current object instance.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.start();
cancel()
Description
Stops the countdown.
Parameters
None
Returns
The current object instance.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.start();
cntdwn.cancel();
pause()
Description
Pauses the countdown.
Parameters
None
Returns
The current object instance.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.start();
cntdwn.pause();
resume()
Description
Resumes the countdown.
Parameters
None
Returns
The current object instance.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.start();
cntdwn.pause();
cntdwn.resume();
getTimeleft()
Description
Checks how much time is left before the countdown ends.
Parameters
None
Returns
The current time of the countdown.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.start();
cntdwn.getTimeleft(); // 5000
getTimeFrom()
Description
Gets the start time of the countdown.
Parameters
None
Returns
The start time of the countdown.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.getTimeFrom(); // 5000
getTimeTo()
Description
Gets the end time of the countdown.
Parameters
None
Returns
The end time of the countdown.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.getTimeTo(); // 0
isPaused()
Description
Checks if the countdown is paused.
Parameters
None
Returns
The boolean of the pause state.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.start();
cntdwn.isPaused(); // false
isRunning()
Description
Checks if the countdown is running.
Parameters
None
Returns
The boolean of the running state.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.start();
cntdwn.isRunning(); // true
setTimeFrom()
Description
Sets the time the countdown will start from. Using this method during the countdown results in canceling the countdown.
Parameters
Time in milliseconds.
Returns
The current object instance.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.setTimeFrom(5000);
setTimeTo()
Description
Sets the time when the countdown will end. Using this method during the countdown results in canceling the countdown.
Parameters
Time in milliseconds.
Returns
The current object instance.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.setTimeTo(0);
setOnTick()
Description
Sets the callback function for the 'tick' event.
Parameters
The callback function with no parameters.
Returns
The current object instance.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.setOnTick(() => {
console.log('Tick!');
});
setOnStart()
Description
Sets the callback function for the 'start' event.
Parameters
The callback function with no parameters.
Returns
The current object instance.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.setOnStart(() => {
console.log('Started!');
});
setOnCancel()
Description
Sets the callback function for the 'cancel' event.
Parameters
The callback function with no parameters.
Returns
The current object instance.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.setOnCancel(() => {
console.log('Canceled!');
});
setOnFinish()
Description
Sets the callback function for the 'finish' event.
Parameters
The callback function with no parameters.
Returns
The current object instance.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.setOnFinish(() => {
console.log('Finished!');
});
setOnPause()
Description
Sets the callback function for the 'pause' event.
Parameters
The callback function with no parameters.
Returns
The current object instance.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.setOnPause(() => {
console.log('Paused!');
});
setOnResume()
Description
Sets the callback function for the 'resume' event.
Parameters
The callback function with no parameters.
Returns
The current object instance.
Example
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn.setOnResume(() => {
console.log('Resumed!');
});
Events
onTick
Description
Triggers the function if set by setOnTick or in the constructor every 10ms.
onStart
Description
Triggers the function if set by setOnStart or in the constructor after running the start() function.
onFinish
Description
Triggers the function if set by setOnFinish or in the constructor after the countdown ends.
onPause
Description
Triggers the function if set by setOnPause or in the constructor after running the pause() function.
onResume
Description
Triggers the function if set by setOnResume or in the constructor after running the resume() function.
onCancel
Description
Triggers the function if set by setOnCancel or in the constructor after running the cancel() function.
Optional chaining
Options which return the current object instance can be chained together like so:
const cntdwn = new Cntdwn({
from: 5000,
to: 0,
});
cntdwn
.setOnStart(() => console.log('Started!'))
.setOnFinish(() => console.log('Finished!'))
.setOnPause(() => console.log('Paused!'))
.setOnResume(() => console.log('Resumed!'))
.setOnCancel(() => console.log('Canceled!'))
.start();
License
Licensed under the MIT license