-
Notifications
You must be signed in to change notification settings - Fork 53
/
example.html
61 lines (50 loc) · 1.65 KB
/
example.html
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
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at https://mozilla.org/MPL/2.0/. -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>server-date Example</title>
</head>
<body>
<h1>server-date Example</h1>
<table border="1">
<tr>
<td>client</td>
<td id="client"></td>
</tr>
<tr>
<td>server</td>
<td id="server"></td>
</tr>
<tr>
<td>difference</td>
<td id="difference" style="text-align: right"></td>
</tr>
</table>
<script type="module">
import { getServerDate } from "./serverDate.js";
let lastSample = {};
const synchronize = async () => {
lastSample = await getServerDate();
};
// Display two real time clocks, the server's and the client's, and show
// the difference between them in milliseconds.
const updateClocks = () => {
const { offset, uncertainty } = lastSample;
const clientDate = new Date();
const serverDate = new Date(clientDate.getTime() + offset);
document.getElementById("client").innerHTML = String(clientDate);
document.getElementById("server").innerHTML = String(serverDate);
document.getElementById(
"difference"
).innerHTML = `${offset} ± ${uncertainty} ms`;
};
synchronize();
setInterval(synchronize, 10 * 60 * 1000);
// Display the clocks and update them every second.
updateClocks();
setInterval(updateClocks, 1000);
</script>
</body>
</html>