File size: 11,690 Bytes
749d9e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ecabe3
 
 
 
 
 
5fa4bd0
 
 
 
 
2ecabe3
 
749d9e0
 
 
2ecabe3
 
749d9e0
2ecabe3
749d9e0
 
 
 
2ecabe3
 
749d9e0
 
 
5fa4bd0
749d9e0
 
2ecabe3
 
749d9e0
 
 
2ecabe3
 
 
749d9e0
 
 
 
2ecabe3
749d9e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fa4bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
749d9e0
 
 
 
 
 
 
 
 
 
5fa4bd0
749d9e0
 
5fa4bd0
749d9e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ecabe3
 
 
 
 
 
749d9e0
517f939
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CountdownSync Pro</title>
    <link rel="icon" type="image/x-icon" href="/static/favicon.ico">
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
    <script src="https://unpkg.com/feather-icons"></script>
    <script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
        
        body {
            font-family: 'Inter', sans-serif;
        }
        
        .timer-container {
            background: linear-gradient(135deg, #fef2f2 0%, #fee2e2 100%);
            backdrop-filter: blur(10px);
        }
        
        .timer-display {
            font-feature-settings: "tnum";
            font-variant-numeric: tabular-nums;
        }
        
        .settings-panel {
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }
        
        .pulse-animation {
            animation: pulse 2s infinite;
        }
        
        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.7; }
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen flex items-center justify-center">
    <div class="container mx-auto px-4">
        <!-- Main Timer Section -->
        <div class="max-w-md mx-auto">
            <div class="timer-container rounded-3xl shadow-2xl p-8 border border-red-100">
                <!-- Timer Display -->
                <div class="text-center mb-8">
                    <h1 class="text-3xl font-bold text-red-600 mb-2">CountdownSync Pro</h1>
                    <p class="text-gray-600 mb-6">Real-time synchronized countdown</p>
                    
                    <div class="timer-display text-7xl font-bold text-red-600 mb-8 bg-white rounded-2xl py-6 px-8 shadow-inner border border-red-200">
                        <span id="minutes">00</span>:<span id="seconds">00</span>
                    </div>
                    
                    <div class="text-sm text-gray-500 mb-2" id="status">
                        Ready to start
                    </div>
                </div>

                <!-- Control Buttons -->
                <div class="flex justify-center space-x-4">
                    <button id="resetBtn" class="bg-red-500 hover:bg-red-600 text-white px-6 py-3 rounded-xl font-semibold shadow-lg transition-all duration-300 transform hover:scale-105 flex items-center space-x-2">
                        <i data-feather="refresh-cw"></i>
                        <span>Reset Timer</span>
                    </button>
                    
                    <button id="settingsBtn" class="bg-gray-700 hover:bg-gray-800 text-white px-6 py-3 rounded-xl font-semibold shadow-lg transition-all duration-300 transform hover:scale-105 flex items-center space-x-2">
                        <i data-feather="settings"></i>
                        <span>Settings</span>
                    </button>
                </div>
            </div>
        </div>

        <!-- Settings Panel -->
        <div id="settingsPanel" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center p-4 hidden z-50">
            <div class="bg-white rounded-3xl shadow-2xl p-8 max-w-sm w-full mx-auto transform transition-all duration-300 scale-95">
                <div class="flex justify-between items-center mb-6">
                    <h2 class="text-2xl font-bold text-gray-800">Timer Settings</h2>
                    <button id="closeSettings" class="text-gray-500 hover:text-gray-700 transition-colors">
                        <i data-feather="x"></i>
                    </button>
                </div>
                
                <div class="space-y-6">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">
                            Duration (minutes)
                        </label>
                        <input type="number" id="durationInput" min="1" max="120" value="5" 
                               class="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-red-500 focus:border-red-500 transition-all duration-300">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">
                            Alert Sound
                        </label>
                        <select id="soundSelect" class="w-full px-4 py-3 border border-gray-300 rounded-xl focus:ring-2 focus:ring-red-500 focus:border-red-500 transition-all duration-300">
                            <option value="beep">Beep</option>
                            <option value="bell">Bell</option>
                            <option value="alarm">Alarm</option>
                        </select>
                    </div>
                    
                    <button id="saveSettings" class="w-full bg-red-500 hover:bg-red-600 text-white py-3 rounded-xl font-semibold shadow-lg transition-all duration-300 transform hover:scale-105">
                        Start Countdown
                    </button>
                </div>
            </div>
        </div>

        <!-- Connection Status -->
        <div class="fixed bottom-4 right-4 flex items-center space-x-2 bg-white rounded-full px-4 py-2 shadow-lg border">
            <div id="connectionStatus" class="w-3 h-3 bg-green-500 rounded-full"></div>
            <span class="text-sm text-gray-600" id="connectionText">Connected</span>
        </div>
    </div>

    <audio id="alertSound" preload="auto">
        <source src="https://assets.mixkit.co/sfx/preview/mixkit-alarm-digital-clock-beep-989.mp3" type="audio/mpeg">
    </audio>

    <script>
        // Socket.io connection
        const socket = io('https://countdown-sync-server.glitch.me');
        
        // DOM Elements
        const minutesElement = document.getElementById('minutes');
        const secondsElement = document.getElementById('seconds');
        const resetBtn = document.getElementById('resetBtn');
        const settingsBtn = document.getElementById('settingsBtn');
        const settingsPanel = document.getElementById('settingsPanel');
        const closeSettings = document.getElementById('closeSettings');
        const durationInput = document.getElementById('durationInput');
        const soundSelect = document.getElementById('soundSelect');
        const saveSettings = document.getElementById('saveSettings');
        const statusElement = document.getElementById('status');
        const connectionStatus = document.getElementById('connectionStatus');
        const connectionText = document.getElementById('connectionText');
        const alertSound = document.getElementById('alertSound');
        // Timer state
        let countdown;
        let totalSeconds = 0;
        let isRunning = false;
        // Add new event listener for timer state
        socket.on('timerState', (data) => {
            totalSeconds = data.remainingSeconds;
            isRunning = data.isRunning;
            updateTimerDisplay(totalSeconds);
            updateStatus();
            
            // Start local countdown if timer is running
            if (isRunning && totalSeconds > 0) {
                startLocalCountdown();
            }
        });
// Socket event listeners
        socket.on('connect', () => {
            connectionStatus.className = 'w-3 h-3 bg-green-500 rounded-full pulse-animation';
            connectionText.textContent = 'Connected';
            // Request current timer state when connecting
            socket.emit('getTimerState');
        });
socket.on('disconnect', () => {
            connectionStatus.className = 'w-3 h-3 bg-red-500 rounded-full';
            connectionText.textContent = 'Disconnected';
        });
        socket.on('timerUpdate', (data) => {
            totalSeconds = data.remainingSeconds;
            updateTimerDisplay(totalSeconds);
            isRunning = data.isRunning;
            updateStatus();
        });
socket.on('timerComplete', () => {
            playAlertSound();
            isRunning = false;
            totalSeconds = 0;
            updateTimerDisplay(totalSeconds);
            statusElement.textContent = 'Timer completed!';
            statusElement.className = 'text-sm text-red-600 font-semibold';
        });
socket.on('timerReset', (data) => {
            totalSeconds = data.duration * 60;
            updateTimerDisplay(totalSeconds);
            isRunning = false;
            statusElement.textContent = 'Timer reset';
            statusElement.className = 'text-sm text-gray-600';
        });
// Event Listeners
        resetBtn.addEventListener('click', () => {
            socket.emit('resetTimer');
        });

        settingsBtn.addEventListener('click', () => {
            settingsPanel.classList.remove('hidden');
            setTimeout(() => {
                settingsPanel.querySelector('div').classList.remove('scale-95');
            }, 10);
        });

        closeSettings.addEventListener('click', closeSettingsPanel);
        saveSettings.addEventListener('click', saveTimerSettings);
        // Functions
        function updateTimerDisplay(totalSeconds) {
            const minutes = Math.floor(totalSeconds / 60);
            const seconds = totalSeconds % 60;
            
            minutesElement.textContent = minutes.toString().padStart(2, '0');
            secondsElement.textContent = seconds.toString().padStart(2, '0');
        }

        function startLocalCountdown() {
            // Clear any existing countdown
            if (countdown) {
                clearInterval(countdown);
            }
            
            // Start local countdown
            countdown = setInterval(() => {
                if (totalSeconds > 0 && isRunning) {
                    totalSeconds--;
                    updateTimerDisplay(totalSeconds);
                } else {
                    clearInterval(countdown);
                    isRunning = false;
                    updateStatus();
                }
            }, 1000);
        }
function closeSettingsPanel() {
            settingsPanel.querySelector('div').classList.add('scale-95');
            setTimeout(() => {
                settingsPanel.classList.add('hidden');
            }, 300);
        }
        function saveTimerSettings() {
            const duration = parseInt(durationInput.value);
            if (duration > 0) {
                socket.emit('startTimer', { duration });
                closeSettingsPanel();
                startLocalCountdown();
            }
        }
function playAlertSound() {
            alertSound.currentTime = 0;
            alertSound.play().catch(e => console.log('Audio play failed:', e));
        }

        function updateStatus() {
            if (isRunning) {
                statusElement.textContent = 'Counting down...';
                statusElement.className = 'text-sm text-green-600 font-semibold';
            } else {
                statusElement.textContent = 'Ready to start';
                statusElement.className = 'text-sm text-gray-600';
            }
        }
        // Initialize
        feather.replace();
        updateTimerDisplay(0);
        
        // Check if socket is connected and request state
        if (socket.connected) {
            socket.emit('getTimerState');
        }
</script>
</body>
</html>