managerbhingong commited on
Commit
847facb
·
verified ·
1 Parent(s): ac02bf1

pastikan timer bisa dilihat live preview di browser lain dengan link yang sama dan tampilkan berapa banyaka yang sedang melihatnya

Browse files
Files changed (1) hide show
  1. index.html +138 -28
index.html CHANGED
@@ -66,12 +66,17 @@
66
  <span>Settings</span>
67
  </button>
68
  </div>
69
-
70
- <!-- Status -->
71
- <div id="status" class="text-center text-secondary text-sm">
72
- Timer Ready - Click Reset to Start
 
 
 
 
 
73
  </div>
74
- </div>
75
 
76
  <!-- Settings Modal -->
77
  <div id="settingsModal" class="fixed inset-0 bg-black/80 flex items-center justify-center hidden z-50">
@@ -127,7 +132,6 @@
127
  <audio id="chimeAlarm" preload="auto">
128
  <source src="https://assets.mixkit.co/active_storage/sfx/255/255-preview.mp3" type="audio/mpeg">
129
  </audio>
130
-
131
  <script>
132
  class Timer {
133
  constructor() {
@@ -137,13 +141,15 @@
137
  this.interval = null;
138
  this.alarmType = 'beep';
139
  this.autoRestart = false;
 
 
140
 
141
  this.initializeElements();
142
  this.attachEventListeners();
 
143
  this.updateDisplay();
144
  }
145
-
146
- initializeElements() {
147
  this.timerDisplay = document.getElementById('timer');
148
  this.statusDisplay = document.getElementById('status');
149
  this.resetBtn = document.getElementById('resetBtn');
@@ -174,12 +180,12 @@
174
  }
175
  });
176
  }
177
-
178
  start() {
179
  if (this.isRunning) return;
180
 
181
  this.isRunning = true;
182
- this.statusDisplay.textContent = 'Timer Running...';
 
183
  this.resetBtn.classList.add('pulse-animation');
184
 
185
  this.interval = setInterval(() => {
@@ -191,38 +197,34 @@
191
  }
192
  }, 1000);
193
  }
194
-
195
- stop() {
196
  this.isRunning = false;
197
  clearInterval(this.interval);
198
  this.resetBtn.classList.remove('pulse-animation');
199
  this.statusDisplay.textContent = 'Timer Paused';
200
  }
201
-
202
  reset() {
203
  this.stop();
204
  this.remainingTime = this.duration;
205
  this.updateDisplay();
206
  this.start();
207
- this.statusDisplay.textContent = 'Timer Started!';
208
  }
209
-
210
  complete() {
211
  this.stop();
212
  this.remainingTime = 0;
213
  this.updateDisplay();
214
  this.triggerAlarm();
215
- this.statusDisplay.textContent = 'Timer Complete! 🎉';
216
 
217
  if (this.autoRestart) {
218
  setTimeout(() => {
219
  this.reset();
220
- this.statusDisplay.textContent = 'Auto-restarted Timer';
221
- }, 2000);
222
  }
223
  }
224
-
225
- triggerAlarm() {
226
  if (this.alarmType === 'none') return;
227
 
228
  const alarm = document.getElementById(`${this.alarmType}Alarm`);
@@ -232,7 +234,109 @@
232
  }
233
  }
234
 
235
- updateDisplay() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  const minutes = Math.floor(this.remainingTime / 60);
237
  const seconds = this.remainingTime % 60;
238
  this.timerDisplay.textContent = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
@@ -272,19 +376,25 @@
272
  this.statusDisplay.textContent = 'Settings Saved!';
273
  }
274
  }
275
-
276
  // Initialize timer when page loads
277
  document.addEventListener('DOMContentLoaded', () => {
278
  feather.replace();
279
  new Timer();
 
 
 
 
 
 
280
 
281
- // Real-time updates for multiple browser windows
282
- if (typeof(Storage) !== "undefined") {
283
- setInterval(() => {
284
- localStorage.setItem('timerLastUpdate', Date.now().toString());
285
- }, 1000);
286
  }
287
  });
288
- </script>
 
 
289
  </body>
290
  </html>
 
66
  <span>Settings</span>
67
  </button>
68
  </div>
69
+ <!-- Status & Live Info -->
70
+ <div class="space-y-2 mb-6">
71
+ <div id="status" class="text-center text-secondary text-sm">
72
+ Timer Ready - Click Reset to Start
73
+ </div>
74
+ <div id="viewers" class="text-center text-secondary text-xs">
75
+ <i data-feather="users" class="w-3 h-3 inline mr-1"></i>
76
+ <span>Loading viewers...</span>
77
+ </div>
78
  </div>
79
+ </div>
80
 
81
  <!-- Settings Modal -->
82
  <div id="settingsModal" class="fixed inset-0 bg-black/80 flex items-center justify-center hidden z-50">
 
132
  <audio id="chimeAlarm" preload="auto">
133
  <source src="https://assets.mixkit.co/active_storage/sfx/255/255-preview.mp3" type="audio/mpeg">
134
  </audio>
 
135
  <script>
136
  class Timer {
137
  constructor() {
 
141
  this.interval = null;
142
  this.alarmType = 'beep';
143
  this.autoRestart = false;
144
+ this.viewers = 1;
145
+ this.lastUpdateTime = Date.now();
146
 
147
  this.initializeElements();
148
  this.attachEventListeners();
149
+ this.initializeRealTimeSync();
150
  this.updateDisplay();
151
  }
152
+ initializeElements() {
 
153
  this.timerDisplay = document.getElementById('timer');
154
  this.statusDisplay = document.getElementById('status');
155
  this.resetBtn = document.getElementById('resetBtn');
 
180
  }
181
  });
182
  }
 
183
  start() {
184
  if (this.isRunning) return;
185
 
186
  this.isRunning = true;
187
+ this.lastUpdateTime = Date.now();
188
+ this.statusDisplay.textContent = 'Live Timer Running...';
189
  this.resetBtn.classList.add('pulse-animation');
190
 
191
  this.interval = setInterval(() => {
 
197
  }
198
  }, 1000);
199
  }
200
+ stop() {
 
201
  this.isRunning = false;
202
  clearInterval(this.interval);
203
  this.resetBtn.classList.remove('pulse-animation');
204
  this.statusDisplay.textContent = 'Timer Paused';
205
  }
 
206
  reset() {
207
  this.stop();
208
  this.remainingTime = this.duration;
209
  this.updateDisplay();
210
  this.start();
211
+ this.statusDisplay.textContent = 'Live Timer Started!';
212
  }
 
213
  complete() {
214
  this.stop();
215
  this.remainingTime = 0;
216
  this.updateDisplay();
217
  this.triggerAlarm();
218
+ this.statusDisplay.textContent = 'Live Timer Complete! 🎉';
219
 
220
  if (this.autoRestart) {
221
  setTimeout(() => {
222
  this.reset();
223
+ this.statusDisplay.textContent = 'Live Auto-restarted Timer';
224
+ }, 2000);
225
  }
226
  }
227
+ triggerAlarm() {
 
228
  if (this.alarmType === 'none') return;
229
 
230
  const alarm = document.getElementById(`${this.alarmType}Alarm`);
 
234
  }
235
  }
236
 
237
+ initializeRealTimeSync() {
238
+ // Sync timer state across multiple browser instances
239
+ this.syncInterval = setInterval(() => {
240
+ this.syncWithOtherInstances();
241
+ }, 1000);
242
+
243
+ // Listen for storage events (other tabs)
244
+ window.addEventListener('storage', (e) => {
245
+ if (e.key === 'timerState') {
246
+ const state = JSON.parse(e.newValue);
247
+ this.syncTimerState(state);
248
+ }
249
+ });
250
+
251
+ // Listen for page visibility changes
252
+ document.addEventListener('visibilitychange', () => {
253
+ if (!document.hidden) {
254
+ this.syncWithOtherInstances();
255
+ }
256
+ });
257
+ }
258
+
259
+ syncWithOtherInstances() {
260
+ const currentTime = Date.now();
261
+
262
+ // Update viewer count
263
+ const activeViewers = JSON.parse(localStorage.getItem('activeViewers') || '{}');
264
+ const viewerKeys = Object.keys(activeViewers);
265
+
266
+ // Remove inactive viewers (more than 5 seconds old)
267
+ Object.keys(activeViewers).forEach(key => {
268
+ if (currentTime - activeViewers[key] > 5000) {
269
+ delete activeViewers[key];
270
+ }
271
+ });
272
+
273
+ // Add/update current viewer
274
+ const viewerId = Math.random().toString(36).substr(2, 9);
275
+ activeViewers[viewerId] = currentTime;
276
+ localStorage.setItem('activeViewers', JSON.stringify(activeViewers));
277
+
278
+ this.viewers = Object.keys(activeViewers).length;
279
+ this.updateViewersDisplay();
280
+
281
+ // Broadcast current timer state
282
+ if (this.isRunning) {
283
+ const elapsed = Math.floor((currentTime - this.lastUpdateTime) / 1000);
284
+ const newRemaining = Math.max(0, this.remainingTime - elapsed);
285
+
286
+ if (newRemaining !== this.remainingTime) {
287
+ this.remainingTime = newRemaining;
288
+ this.lastUpdateTime = currentTime;
289
+ this.updateDisplay();
290
+
291
+ if (this.remainingTime <= 0) {
292
+ this.complete();
293
+ }
294
+ }
295
+ }
296
+
297
+ // Save current state to localStorage for other instances
298
+ const state = {
299
+ remainingTime: this.remainingTime,
300
+ isRunning: this.isRunning,
301
+ duration: this.duration,
302
+ lastUpdate: currentTime
303
+ };
304
+ localStorage.setItem('timerState', JSON.stringify(state));
305
+ }
306
+
307
+ syncTimerState(state) {
308
+ if (!state || !state.lastUpdate) return;
309
+
310
+ const currentTime = Date.now();
311
+ const timeDiff = Math.floor((currentTime - state.lastUpdate) / 1000);
312
+
313
+ if (state.isRunning && this.isRunning !== state.isRunning) {
314
+ // Another instance is controlling the timer
315
+ this.remainingTime = Math.max(0, state.remainingTime - timeDiff);
316
+ this.isRunning = state.isRunning;
317
+ this.duration = state.duration;
318
+ this.lastUpdateTime = state.lastUpdate;
319
+
320
+ if (this.remainingTime <= 0) {
321
+ this.complete();
322
+ } else if (this.isRunning) {
323
+ this.statusDisplay.textContent = 'Live Timer Running...';
324
+ this.resetBtn.classList.add('pulse-animation');
325
+ }
326
+
327
+ this.updateDisplay();
328
+ }
329
+ }
330
+
331
+ updateViewersDisplay() {
332
+ const viewersElement = document.getElementById('viewers');
333
+ if (viewersElement) {
334
+ const viewerText = this.viewers === 1 ? '1 person viewing' : `${this.viewers} people viewing`;
335
+ viewersElement.innerHTML = `<i data-feather="users" class="w-3 h-3 inline mr-1"></i><span>${viewerText}</span>`;
336
+ feather.replace();
337
+ }
338
+ }
339
+ updateDisplay() {
340
  const minutes = Math.floor(this.remainingTime / 60);
341
  const seconds = this.remainingTime % 60;
342
  this.timerDisplay.textContent = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
 
376
  this.statusDisplay.textContent = 'Settings Saved!';
377
  }
378
  }
 
379
  // Initialize timer when page loads
380
  document.addEventListener('DOMContentLoaded', () => {
381
  feather.replace();
382
  new Timer();
383
+ });
384
+
385
+ // Cleanup inactive viewers on page unload
386
+ window.addEventListener('beforeunload', () => {
387
+ const activeViewers = JSON.parse(localStorage.getItem('activeViewers') || '{}');
388
+ const currentTime = Date.now();
389
 
390
+ // Remove viewers older than 10 seconds
391
+ Object.keys(activeViewers).forEach(key => {
392
+ if (currentTime - activeViewers[key] > 10000) {
393
+ delete activeViewers[key];
 
394
  }
395
  });
396
+ localStorage.setItem('activeViewers', JSON.stringify(activeViewers));
397
+ });
398
+ </script>
399
  </body>
400
  </html>