Improve count down accuracy

This commit is contained in:
soruly 2022-06-20 14:28:58 +00:00
parent 287f46b976
commit cef1b6f5aa
No known key found for this signature in database
GPG Key ID: EF971E90F3D2521F
2 changed files with 9 additions and 6 deletions

View File

@ -192,12 +192,15 @@ app.get("/", async (req, res) => {
res.write("retry: 1000\n\n");
while (true) {
res.write(
`data: ${JSON.stringify(
JSON.parse(fs.readFileSync("data/latest.json")).map(({ name, otp }) => ({
`data: ${JSON.stringify({
nextUpdate:
(Math.floor(Math.round(new Date().getTime() / 1000.0) / 30) + 1) * 30 * 1000 -
new Date().getTime(),
list: JSON.parse(fs.readFileSync("data/latest.json")).map(({ name, otp }) => ({
name,
otp: getOtp(otp),
}))
)}\n\n`
})),
})}\n\n`
);
await new Promise((resolve) =>

View File

@ -10,12 +10,12 @@ events.onmessage = async (event) => {
{ transform: `scaleX(0)`, transformOrigin: "left" },
],
{
duration: animation ? 30000 : Math.ceil(Date.now() / 30000) * 30000 - Date.now(),
duration: JSON.parse(event.data).nextUpdate,
iterations: 1,
easing: "linear",
}
);
for (const { name, otp } of JSON.parse(event.data)) {
for (const { name, otp } of JSON.parse(event.data).list) {
document.querySelector(`#${name} .otp`).innerText = otp;
}
};