обратный отсчёт с нулями
function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
var total_items = 50;
var d = new Date();
var min_items_left = 8;
var max_items_left = 12;
let counterItems = null;
if (false && sessionStorage) {
counterItems = sessionStorage.getItem(StorageKeys.counterItems);
}
var remaining_items = counterItems ? Number(counterItems) : randomIntFromInterval(min_items_left, max_items_left);
var min_of_remaining_items = 3;
var decrease_after = 1.7;
var decrease_after_first_item = 0.17;
jQuery.noConflict()(function($) {
$(document).ready(function() {
// $("#progress_bar").progressbar();
// var tag = "ctdn-12-12".match(/\d+/g);
var hour = 14;
var theDaysBox = $("#numdays");
var theHoursBox = $("#numhours");
var theMinsBox = $("#nummins");
var theSecsBox = $("#numsecs");
var d = new Date();
var n = d.getDay();
var date = 7;
var gg = 14;
var hh = 12;
var ii = 30;
var nsec = 0 - d.getSeconds();
if (nsec < 0) {
nsec = 60 - d.getSeconds();
gg = 1
}
var nmin = 0 - d.getMinutes() - gg;
if (nmin < 0) {
nmin = 60 - d.getMinutes() - gg;
hh = 1
}
var nhrs = 14 - d.getHours() - hh;
if (nhrs < 0) {
nhrs = 38 - d.getHours() - hh;
ii = 1
}
var ndat = date - 1;
if (ndat < 0) {
var mmon = d.getMonth();
ndat = 30 + date - d.getDate() - ii
}
let counterData = null;
if (false && sessionStorage) {
counterData = JSON.parse(sessionStorage.getItem(StorageKeys.counter));
}
// use the stored valued value if present
theSecsBox.html(counterData ? counterData.nsec : nsec);
theMinsBox.html(counterData ? counterData.nmin : nmin);
theHoursBox.html(counterData ? counterData.nhrs : nhrs);
theDaysBox.html(counterData ? counterData.ndat : ndat);
var refreshId = setInterval(function() {
var e = theSecsBox.text();
var a = theMinsBox.text();
var c = theHoursBox.text();
var b = theDaysBox.text();
if (false && sessionStorage) {
const currentValues = { nsec: e, nmin : a, nhrs : c, ndat : b };
sessionStorage.setItem(StorageKeys.counter, JSON.stringify(currentValues));
}
if (e == 0 && a == 0 && c == 0 && b == 0) {}
else {
if (e == 0 && a == 0 && c == 0) {
theDaysBox.html(b - 1);
theHoursBox.html("23");
theMinsBox.html("59");
theSecsBox.html("59")
}
else {
if (e == 0 && a == 0) {
theHoursBox.html(c - 1);
theMinsBox.html("59");
theSecsBox.html("59")
}
else {
if (e == 0) {
theMinsBox.html(a - 1);
theSecsBox.html("59")
}
else {
theSecsBox.html(e - 1)
}
}
}
}
}, 1000);
});
});
На данный момент нуля отсеиваются, нужно что бы нули остались, например 06 дней, 07 часов, 02 минуты, 08 секунд
Ответы (1 шт):
Автор решения: Павел Ериков
→ Ссылка
Вообще лучше работать через переменные, а не через text() элементов.
Вот пример измененного кода:
var refreshId = setInterval(function() {
if (false && sessionStorage) {
const currentValues = { nsec: nsec, nmin : nmin, nhrs : nhrs, ndat : ndat };
sessionStorage.setItem(StorageKeys.counter, JSON.stringify(currentValues));
}
if (nsec == 0 && nmin == 0 && nhrs == 0 && ndat == 0) {}
else {
if (nsec == 0 && nmin == 0 && nhrs == 0) {
ndat = ndat - 1;
nhrs = 23;
nmin = 59;
nsec = 59;
}
else {
if (nsec == 0 && nmin == 0) {
nhrs = nhrs - 1;
nmin = 59;
nsec = 59;
}
else {
if (nsec == 0) {
nmin = nmin - 1;
nsec = 59;
}
else {
nsec = nsec - 1;
}
}
}
}
theDaysBox.html(ndat >= 10 ? ndat : '0' + ndat);
theHoursBox.html(nhrs >= 10 ? nhrs : '0' + nhrs);
theMinsBox.html(nmin >= 10 ? nmin : '0' + nmin);
theSecsBox.html(nsec >= 10 ? nsec : '0' + nsec);
}, 1000);