Skip to content

Commit 80c13ce

Browse files
committed
Enforced character limit, improved countdown readability and added flash animation
1 parent aa07dec commit 80c13ce

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

scripts/autoRefresh.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ function startAutoRefreshWorker() {
1212
autoRefreshWorker.addEventListener('message', (event) => {
1313
const data = event.data;
1414
if (data.type === 'countdown') {
15-
const toggleText = document.querySelector(".toggle-text");
16-
toggleText.textContent = `Auto Refresh (${data.timeLeft}s)`;
15+
const refreshInput = document.getElementById("refresh-time-input");
16+
if (isAutoRefreshEnabled && refreshInput) {
17+
refreshInput.value = data.timeLeft;
18+
refreshInput.readOnly = true;
19+
20+
// Flash effect
21+
refreshInput.classList.remove("flash"); // Reset if already applied
22+
void refreshInput.offsetWidth; // Force reflow to restart the animation
23+
refreshInput.classList.add("flash");
24+
}
1725
timeLeft = data.timeLeft; // Update timeLeft in the main thread
1826
} else if (data.type === 'fetch') {
1927
fetchStockData();
@@ -35,13 +43,17 @@ function stopAutoRefreshWorker() {
3543
// Function to toggle auto-refresh
3644
function toggleAutoRefresh() {
3745
const autoRefreshCheckbox = document.getElementById("auto-refresh-checkbox");
46+
const refreshInput = document.getElementById("refresh-time-input");
47+
3848
if (autoRefreshCheckbox.checked) {
3949
isAutoRefreshEnabled = true;
50+
refreshInput.readOnly = true;
4051
if (autoRefreshWorker) {
4152
autoRefreshWorker.postMessage({ type: 'start', duration: countdownDuration, timeLeft });
4253
}
4354
} else {
4455
isAutoRefreshEnabled = false;
56+
refreshInput.readOnly = false;
4557
if (autoRefreshWorker) {
4658
autoRefreshWorker.postMessage({ type: 'stop' });
4759
}
@@ -63,6 +75,18 @@ function handleRefreshTimeInput() {
6375
});
6476
}
6577

78+
// Function to limit user input characters
79+
document.addEventListener('DOMContentLoaded', () => {
80+
const input = document.getElementById('refresh-time-input');
81+
const maxLength = 2;
82+
83+
input.addEventListener('input', () => {
84+
if (input.value.length > maxLength) {
85+
input.value = input.value.slice(0, maxLength);
86+
}
87+
});
88+
});
89+
6690
// Initialize the refresh time input handler
6791
handleRefreshTimeInput();
6892

scripts/autoRefreshWorker.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ self.addEventListener('message', (event) => {
1515
});
1616

1717
function startCountdown() {
18-
clearInterval(countdownInterval); // Clear any existing interval
19-
countdownInterval = setInterval(() => {
20-
timeLeft -= 1;
21-
self.postMessage({ type: 'countdown', timeLeft });
18+
clearInterval(countdownInterval);
2219

23-
if (timeLeft <= 0) {
20+
// Immediately send the initial timeLeft before any decrement
21+
self.postMessage({ type: 'countdown', timeLeft });
22+
23+
countdownInterval = setInterval(() => {
24+
if (timeLeft > 1) {
25+
timeLeft -= 1;
26+
self.postMessage({ type: 'countdown', timeLeft });
27+
} else {
28+
self.postMessage({ type: 'countdown', timeLeft });
2429
self.postMessage({ type: 'fetch' });
25-
timeLeft = countdownDuration; // Reset the countdown
30+
timeLeft = countdownDuration;
31+
self.postMessage({ type: 'countdown', timeLeft });
2632
}
2733
}, 1000);
2834
}
2935

3036
function stopCountdown() {
3137
clearInterval(countdownInterval);
32-
self.postMessage({ type: 'countdown', timeLeft }); // Send the current timeLeft back to the main thread
3338
}

0 commit comments

Comments
 (0)