Skip to content

Commit

Permalink
feat(util): add timeOut func
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Dec 10, 2024
1 parent 2022f51 commit 13047b4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion js/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion js/guest.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const guest = (() => {

theme.spyTop();
theme.showButtonChangeTheme();
setTimeout(animation, 1500);
util.timeOut(animation, 1500);
};

const init = () => {
Expand Down
6 changes: 4 additions & 2 deletions js/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -93,6 +93,8 @@ export const request = (method, path) => {

document.body.removeChild(link);
window.URL.revokeObjectURL(href);

return true;
})
.catch((err) => {
alert(err);
Expand Down
42 changes: 24 additions & 18 deletions js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -90,14 +100,9 @@ export const util = (() => {
const tmp = button.innerHTML;
button.innerHTML = message ? message : '<i class="fa-solid fa-check"></i>';

let clear = null;
clear = setTimeout(() => {
timeOut(() => {
button.disabled = false;
button.innerHTML = tmp;

clearTimeout(clear);
clear = null;
return;
}, timeout);
};

Expand All @@ -122,6 +127,7 @@ export const util = (() => {
copy,
close,
modal,
timeOut,
opacity,
animate,
escapeHtml,
Expand Down

0 comments on commit 13047b4

Please sign in to comment.