From 13047b4ce2509da85b5574ac29a9bf56562bb96d Mon Sep 17 00:00:00 2001 From: dewanakl Date: Tue, 10 Dec 2024 22:53:31 +0700 Subject: [PATCH] feat(util): add timeOut func --- js/card.js | 2 +- js/guest.js | 2 +- js/request.js | 6 ++++-- js/util.js | 42 ++++++++++++++++++++++++------------------ 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/js/card.js b/js/card.js index 81f18b1..041b785 100644 --- a/js/card.js +++ b/js/card.js @@ -13,7 +13,7 @@ export const card = (() => { let tracker = null; let showHide = null; - const maxCommentLength = 150; + const maxCommentLength = 250; const renderLoading = () => { const comments = document.getElementById('comments'); diff --git a/js/guest.js b/js/guest.js index cdbb497..77979e0 100644 --- a/js/guest.js +++ b/js/guest.js @@ -113,7 +113,7 @@ export const guest = (() => { theme.spyTop(); theme.showButtonChangeTheme(); - setTimeout(animation, 1500); + util.timeOut(animation, 1500); }; const init = () => { diff --git a/js/request.js b/js/request.js index af1a0b9..5241552 100644 --- a/js/request.js +++ b/js/request.js @@ -76,8 +76,8 @@ export const request = (method, path) => { return res.blob().then((blob) => ({ blob, filename })); }) .then((res) => { - if (!res) { - return null; + if (res === null) { + return false; } const { blob, filename } = res; @@ -93,6 +93,8 @@ export const request = (method, path) => { document.body.removeChild(link); window.URL.revokeObjectURL(href); + + return true; }) .catch((err) => { alert(err); diff --git a/js/util.js b/js/util.js index d3c2750..7f1db7f 100644 --- a/js/util.js +++ b/js/util.js @@ -8,16 +8,19 @@ export const util = (() => { let op = parseInt(element.style.opacity); let clear = null; - clear = setInterval(() => { + const callback = () => { if (op > 0) { - element.style.opacity = op.toString(); + element.style.opacity = op.toFixed(3); op -= speed; - } else { - clearInterval(clear); - clear = null; - element.remove(); + return; } - }, 10); + + clearInterval(clear); + clear = null; + element.remove(); + }; + + clear = setInterval(callback, 10); }; const escapeHtml = (unsafe) => { @@ -29,6 +32,17 @@ export const util = (() => { .replace(/'/g, '''); }; + const timeOut = (callback, timeout) => { + let clear = null; + const c = () => { + callback(); + clearTimeout(clear); + clear = null; + }; + + clear = setTimeout(c, timeout); + }; + const disableButton = (button, message = 'Loading..') => { button.disabled = true; @@ -58,11 +72,7 @@ export const util = (() => { }; }; - const animate = (svg, timeout, classes) => { - setTimeout(() => { - svg.classList.add(classes); - }, timeout); - }; + const animate = (svg, timeout, classes) => timeOut(() => svg.classList.add(classes), timeout); const modal = (img) => { document.getElementById('show-modal-image').src = img.src; @@ -90,14 +100,9 @@ export const util = (() => { const tmp = button.innerHTML; button.innerHTML = message ? message : ''; - let clear = null; - clear = setTimeout(() => { + timeOut(() => { button.disabled = false; button.innerHTML = tmp; - - clearTimeout(clear); - clear = null; - return; }, timeout); }; @@ -122,6 +127,7 @@ export const util = (() => { copy, close, modal, + timeOut, opacity, animate, escapeHtml,