|
@@ -14,15 +14,41 @@ main.appendChild(heading);
|
|
main.appendChild(app);
|
|
main.appendChild(app);
|
|
main.appendChild(message);
|
|
main.appendChild(message);
|
|
|
|
|
|
|
|
+const checkCounter = () => {
|
|
|
|
+ const intervalId = setInterval(async () => {
|
|
|
|
+ const counter = await api.getCounter();
|
|
|
|
+ if (counter.error) {
|
|
|
|
+ clearInterval(intervalId);
|
|
|
|
+ showErrorMessage(counter.error);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ heading.innerText = counter.name;
|
|
|
|
+ if (counter.state === 'ready') {
|
|
|
|
+ hideMessage();
|
|
|
|
+ const period = new Date(counter.minutes * 1000 * 60);
|
|
|
|
+ render(Countdown.getTime(period));
|
|
|
|
+ } else if (counter.state === 'completed') {
|
|
|
|
+ const zero = new Date(0);
|
|
|
|
+ showMessage();
|
|
|
|
+ render(Countdown.getTime(zero));
|
|
|
|
+ } else {
|
|
|
|
+ clearInterval(intervalId);
|
|
|
|
+ const countdownTimer = new Countdown(
|
|
|
|
+ getEndTime(counter),
|
|
|
|
+ render,
|
|
|
|
+ complete
|
|
|
|
+ )
|
|
|
|
+ }
|
|
|
|
+ }, 1000)
|
|
|
|
+}
|
|
|
|
+
|
|
const api = new fetchObject(entrypoint, main.dataset.id);
|
|
const api = new fetchObject(entrypoint, main.dataset.id);
|
|
-constcheckCounter();
|
|
|
|
|
|
+checkCounter();
|
|
|
|
|
|
-// Get the new year
|
|
|
|
const getEndTime = (counter) => {
|
|
const getEndTime = (counter) => {
|
|
return new Date(Date.parse(counter.endTime));
|
|
return new Date(Date.parse(counter.endTime));
|
|
};
|
|
};
|
|
|
|
|
|
-
|
|
|
|
const format = (t) => {
|
|
const format = (t) => {
|
|
return t < 10 ? '0' + t : t;
|
|
return t < 10 ? '0' + t : t;
|
|
};
|
|
};
|
|
@@ -64,7 +90,7 @@ const hideMessage = () => {
|
|
|
|
|
|
const complete = () => {
|
|
const complete = () => {
|
|
showMessage();
|
|
showMessage();
|
|
- constcheckCounter();
|
|
|
|
|
|
+ checkCounter();
|
|
};
|
|
};
|
|
|
|
|
|
const showErrorMessage = (error) => {
|
|
const showErrorMessage = (error) => {
|
|
@@ -74,35 +100,6 @@ const showErrorMessage = (error) => {
|
|
app.appendChild(buttonRestart);
|
|
app.appendChild(buttonRestart);
|
|
buttonRestart.addEventListener('click', (e) => {
|
|
buttonRestart.addEventListener('click', (e) => {
|
|
buttonRestart.remove();
|
|
buttonRestart.remove();
|
|
- constcheckCounter();
|
|
|
|
|
|
+ checkCounter();
|
|
})
|
|
})
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
-function constcheckCounter () {
|
|
|
|
- const intervalId = setInterval(async () => {
|
|
|
|
- const counter = await api.getCounter();
|
|
|
|
- if (counter.error) {
|
|
|
|
- clearInterval(intervalId);
|
|
|
|
- showErrorMessage(counter.error);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- heading.innerText = counter.name;
|
|
|
|
- if (counter.state === 'ready') {
|
|
|
|
- hideMessage();
|
|
|
|
- const period = new Date(counter.minutes * 1000 * 60);
|
|
|
|
- render(Countdown.getTime(period));
|
|
|
|
- } else if (counter.state === 'completed') {
|
|
|
|
- const zero = new Date(0);
|
|
|
|
- showMessage();
|
|
|
|
- render(Countdown.getTime(zero));
|
|
|
|
- } else {
|
|
|
|
- clearInterval(intervalId);
|
|
|
|
- const countdownTimer = new Countdown(
|
|
|
|
- getEndTime(counter),
|
|
|
|
- render,
|
|
|
|
- complete
|
|
|
|
- )
|
|
|
|
- }
|
|
|
|
- }, 1000)
|
|
|
|
-}
|
|
|