With this plugin, you can easily make a stopwatch or timer on your site. Just init, style and enjoy.
- Typescript support
- Support all environments
- Easy customization
- Simple and lightweight
We support all platforms.
For module bundlers such as Webpack or Browserify.
npm i timezz
Download and install with script
.
<script src="timezz.min.js"></script>
Recommended for learning purposes, you can use the latest version:
<script src="https://cdn.jsdelivr.net/npm/timezz/dist/timezz.min.js"></script>
Recommended for production for avoiding unexpected breakage from newer versions:
<script src="https://cdn.jsdelivr.net/npm/timezz@7.0.0/dist/timezz.min.js"></script>
For native ES Modules, there is also an ES Modules compatible build:
<script type="module">
import timezz from 'https://cdn.jsdelivr.net/npm/timezz@7.0.0/dist/timezz.min.js';
</script>
Here is a base HTML markup for your timer/stopwatch. Main part of HTML are data
attributes for render numbers of years
, days
, hours
, minutes
, seconds
. Every data
attribute isn't mandatory, TimezZ will recalculate time to smaller numbers.
For example:
- if you don't have years, a timer will add these years to days
- if you don't have days, a timer will add these days to hours
- and so on
<div class="timer">
Years: <div data-years></div>
Days: <div data-days></div>
Hours: <div data-hours></div>
Minutes: <div data-minutes></div>
Seconds: <div data-seconds></div>
</div>
TimezZ as an ES6 module.
import timezz from 'timezz';
timezz(document.querySelector('.timer'), {
date: new Date(),
});
TimezZ as a Node.js module
const timezz = require('timezz');
timezz(document.querySelector('.timer'), {
date: new Date(),
});
Exports a global variable called timezz
. Use it like this
<script>
timezz(document.querySelector('.timer'), {
date: new Date(),
});
</script>
TimezZ as an AMD module. Use with Require.js, System.js, and so on.
requirejs(['timezz'], function(timezz) {
timezz(document.querySelector('.timer'), {
date: new Date(),
});
});
Full config with filled params:
timezz(document.querySelector('.timer'), {
date: new Date(),
pause: false,
stopOnZero: true,
beforeCreate() {},
beforeUpdate() {},
update(event) {},
});
- type:
HTMLElement
- required
true
timezz(document.querySelector('.timer'), { date: new Date() });
Date from and to which you want to count. Preferred Date
.
- type:
Date | string | number
- required
true
// Date instance
new Date('1996-05-27 03:15');
// String
'1996-05-27 03:15'
// Timestamp
833156100000
Is the timer can updating every second?
- type:
boolean
- default:
false
- required
false
Can update after initialization.
const timer = timezz(document.querySelector('.timer'), {
date: new Date(),
});
timer.pause = true;
Should the timer continue after end of date point? Only for date in future.
- type:
boolean
- default:
true
- required
false
Can update after initialization.
const timer = timezz(document.querySelector('.timer'), {
date: new Date(),
});
timer.stopOnZero = false;
The function will be called before instance initialization
- type:
function
- default:
undefined
- required
false
Can set after initialization.
const timer = timezz(document.querySelector('.timer'), {
date: new Date(),
});
timer.beforeCreate = () => {};
The function will be called on before each second with an event.
- type:
function
- default:
undefined
- required
false
Can set after initialization.
const timer = timezz(document.querySelector('.timer'), {
date: new Date(),
});
timer.beforeUpdate = () => {};
The function will be called on each second with an event.
- type:
function
- default:
undefined
- required
false
Can set after initialization.
const timer = timezz(document.querySelector('.timer'), {
date: new Date(),
});
timer.update = (event) => {};
const timer = timezz(document.querySelector('.timer'), {
date: new Date(),
});
timer.destroy();
After destroy you can init instance again.
const timer = timezz(document.querySelector('.timer'), {
date: new Date(),
});
timer.destroy();
setTimeout(() => {
timer.init();
}, 1000);
The interface can be declared as a type of instance.
import timezz, { Timezz } from 'timezz';
const plugins: {
timezz: Timezz,
} = {
timezz: null,
};
plugins.timezz = timezz(document.querySelector('.timer'), { date: new Date('1996-05-27 03:15') });
The interface will be sent on each call of the update
method.
import { UpdateEvent } from 'timezz';
const data: {
info: UpdateEvent | null,
} = {
info: null,
};
const timer = timezz(document.querySelector('.timer'), {
date: new Date('1996-05-27 03:15'),
update(event) {
data.info = event;
},
});