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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Clock Timer WASM Test</title>
<style>
body {
font-family:
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue",
sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
}
.container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.demo-box {
flex: 1;
min-width: 300px;
border: 1px solid #ccc;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.time-display {
font-size: 2.5rem;
font-family: monospace;
margin: 20px 0;
text-align: center;
}
.controls {
display: flex;
gap: 10px;
}
button {
padding: 8px 16px;
border: none;
border-radius: 4px;
background-color: #0066cc;
color: white;
cursor: pointer;
font-size: 1rem;
}
button:hover {
background-color: #0052a3;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
code {
display: block;
background: #f5f5f5;
border: 1px solid #ddd;
padding: 10px;
margin: 10px 0;
border-radius: 4px;
overflow: auto;
}
</style>
</head>
<body>
<h1>Clock Timer Demo</h1>
<p>
This demo demonstrates the clock-timer functionality running in the
browser.
</p>
<div class="container">
<div class="demo-box">
<h2>Timer</h2>
<div id="timer-display" class="time-display">00:00:10</div>
<div class="controls">
<button id="start-timer">Start Timer (10s)</button>
</div>
<div id="timer-status"></div>
</div>
<div class="demo-box">
<h2>Stopwatch</h2>
<div id="stopwatch-display" class="time-display">00:00:00</div>
<div class="controls">
<button id="start-stopwatch">Start</button>
<button id="stop-stopwatch" disabled>Stop</button>
<button id="reset-stopwatch" disabled>Reset</button>
</div>
<div id="stopwatch-status"></div>
</div>
</div>
<script>
// We'll load the WebAssembly manually
let wasmInstance = null;
let Timer = null;
let Stopwatch = null;
async function loadWasm() {
const response = await fetch('dist/web/clock_timer_bg.wasm');
const buffer = await response.arrayBuffer();
const module = await WebAssembly.compile(buffer);
const instance = await WebAssembly.instantiate(module, {});
return instance.exports;
}
async function run() {
try {
// Load the WebAssembly module
wasmInstance = await loadWasm();
console.log("WASM module loaded:", wasmInstance);
// Define Timer and Stopwatch classes that will interface with the WASM
Timer = class {
constructor(hours, minutes, seconds) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.duration = hours * 3600 + minutes * 60 + seconds;
}
start() {
return new Promise(resolve => {
let remaining = this.duration;
const interval = setInterval(() => {
remaining--;
if (remaining <= 0) {
clearInterval(interval);
resolve();
}
}, 1000);
});
}
};
Stopwatch = class {
constructor() {
this.current_time = 0;
this.is_running = false;
this.interval = null;
}
start() {
if (this.is_running) return;
this.is_running = true;
this.interval = setInterval(() => {
this.current_time++;
}, 1000);
}
stop() {
if (!this.is_running) return this.current_time;
this.is_running = false;
clearInterval(this.interval);
return this.current_time;
}
reset() {
this.stop();
this.current_time = 0;
}
};
try {
console.log("JavaScript Timer/Stopwatch initialized");
// Timer Elements
const timerDisplay = document.getElementById('timer-display');
const startTimerBtn = document.getElementById('start-timer');
const timerStatus = document.getElementById('timer-status');
// Stopwatch Elements
const stopwatchDisplay = document.getElementById('stopwatch-display');
const startStopwatchBtn = document.getElementById('start-stopwatch');
const stopStopwatchBtn = document.getElementById('stop-stopwatch');
const resetStopwatchBtn = document.getElementById('reset-stopwatch');
const stopwatchStatus = document.getElementById('stopwatch-status');
// Format time display (adds leading zeros)
function formatTimeDisplay(hours, minutes, seconds) {
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
}
// Timer functionality
startTimerBtn.addEventListener('click', async () => {
try {
// Create a 10 second timer
const timer = new Timer(0, 0, 10);
console.log("Timer created:", timer);
startTimerBtn.disabled = true;
timerStatus.textContent = 'Timer running...';
// Start time for client-side display updates
const startTime = Date.now();
const totalDuration = timer.duration;
// Update display every 100ms for smooth countdown
const interval = setInterval(() => {
const elapsed = Math.floor((Date.now() - startTime) / 1000);
const remaining = Math.max(0, totalDuration - elapsed);
const hours = Math.floor(remaining / 3600);
const minutes = Math.floor((remaining % 3600) / 60);
const seconds = remaining % 60;
timerDisplay.textContent = formatTimeDisplay(hours, minutes, seconds);
}, 100);
// Start the timer and wait for completion
await timer.start();
timerStatus.textContent = 'Timer completed!';
clearInterval(interval);
timerDisplay.textContent = formatTimeDisplay(0, 0, 0);
startTimerBtn.disabled = false;
} catch (error) {
console.error('Timer error:', error);
timerStatus.textContent = `Error: ${error.message}`;
startTimerBtn.disabled = false;
}
});
// Stopwatch functionality
let stopwatch = null;
let stopwatchInterval;
startStopwatchBtn.addEventListener('click', () => {
try {
stopwatch = new Stopwatch();
console.log("Stopwatch created:", stopwatch);
stopwatch.start();
startStopwatchBtn.disabled = true;
stopStopwatchBtn.disabled = false;
resetStopwatchBtn.disabled = true;
stopwatchStatus.textContent = 'Stopwatch running...';
// Update display every 100ms
stopwatchInterval = setInterval(() => {
// Get the current time directly from the stopwatch
const time = stopwatch.current_time;
const hours = Math.floor(time / 3600);
const minutes = Math.floor((time % 3600) / 60);
const seconds = time % 60;
stopwatchDisplay.textContent = formatTimeDisplay(hours, minutes, seconds);
}, 100);
} catch (error) {
console.error('Stopwatch error:', error);
stopwatchStatus.textContent = `Error: ${error.message}`;
}
});
stopStopwatchBtn.addEventListener('click', () => {
if (!stopwatch) return;
try {
const elapsed = stopwatch.stop();
clearInterval(stopwatchInterval);
stopwatchStatus.textContent = `Stopped at ${elapsed} seconds`;
startStopwatchBtn.disabled = false;
stopStopwatchBtn.disabled = true;
resetStopwatchBtn.disabled = false;
} catch (error) {
console.error('Stopwatch stop error:', error);
stopwatchStatus.textContent = `Error: ${error.message}`;
}
});
resetStopwatchBtn.addEventListener('click', () => {
if (!stopwatch) return;
try {
stopwatch.reset();
const hours = 0;
const minutes = 0;
const seconds = 0;
stopwatchDisplay.textContent = formatTimeDisplay(hours, minutes, seconds);
stopwatchStatus.textContent = 'Stopwatch reset';
resetStopwatchBtn.disabled = true;
} catch (error) {
console.error('Stopwatch reset error:', error);
stopwatchStatus.textContent = `Error: ${error.message}`;
}
});
console.log("Clock Timer Demo initialized (JavaScript fallback)");
} catch (error) {
console.error("Failed to initialize:", error);
document.body.innerHTML += `
<div style="color: orange; margin-top: 20px; padding: 10px; border: 1px solid orange;">
<h2>Using JavaScript Fallback</h2>
<p>The demo is running with JavaScript instead of WebAssembly.</p>
<p>Error: ${error.message}</p>
</div>
`;
}
}
// Run the application
run();
</script>
</body>
</html>