init
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
define([], function () {
|
||||
'use strict';
|
||||
|
||||
return function (config, element) {
|
||||
var end = new Date(element.getAttribute('data-end')).getTime();
|
||||
var timer;
|
||||
|
||||
function setValue(unit, value) {
|
||||
var target = element.querySelector('[data-time="' + unit + '"]');
|
||||
if (target) {
|
||||
target.textContent = String(Math.max(0, value)).padStart(2, '0');
|
||||
}
|
||||
}
|
||||
|
||||
function update() {
|
||||
var distance = Math.max(0, end - Date.now());
|
||||
|
||||
setValue('days', Math.floor(distance / 86400000));
|
||||
setValue('hours', Math.floor((distance % 86400000) / 3600000));
|
||||
setValue('minutes', Math.floor((distance % 3600000) / 60000));
|
||||
setValue('seconds', Math.floor((distance % 60000) / 1000));
|
||||
|
||||
if (distance === 0 && timer) {
|
||||
window.clearInterval(timer);
|
||||
}
|
||||
}
|
||||
|
||||
if (!Number.isFinite(end)) {
|
||||
return;
|
||||
}
|
||||
|
||||
update();
|
||||
timer = window.setInterval(update, 1000);
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user