mirror of
https://github.com/coalaura/whiskr.git
synced 2025-12-02 20:22:52 +00:00
improve time rendering
This commit is contained in:
@@ -261,7 +261,7 @@
|
|||||||
this.#_time = make("div", "time");
|
this.#_time = make("div", "time");
|
||||||
|
|
||||||
if (this.#time) {
|
if (this.#time) {
|
||||||
this.#_time.textContent = `${this.#time.toFixed(1)}s`;
|
this.#_time.textContent = formatMilliseconds(this.#time * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
_body.appendChild(this.#_time);
|
_body.appendChild(this.#_time);
|
||||||
@@ -868,7 +868,7 @@
|
|||||||
this.#time = time;
|
this.#time = time;
|
||||||
|
|
||||||
if (this.#time) {
|
if (this.#time) {
|
||||||
this.#_time.textContent = `${this.#time.toFixed(1)}s`;
|
this.#_time.textContent = formatMilliseconds(this.#time * 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,16 +46,25 @@ function escapeHtml(text) {
|
|||||||
return text.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
return text.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fracZerosRgx = /(?:(\.\d*?[1-9])0+|\.0+)$/;
|
||||||
|
|
||||||
|
function round(num, digits) {
|
||||||
|
return num.toFixed(digits).replace(fracZerosRgx, "$1") || "0";
|
||||||
|
}
|
||||||
|
|
||||||
function formatMilliseconds(ms) {
|
function formatMilliseconds(ms) {
|
||||||
if (ms < 1000) {
|
if (ms < 1000) {
|
||||||
return `${ms}ms`;
|
return `${ms}ms`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ms < 10000) {
|
if (ms < 60000) {
|
||||||
return `${(ms / 1000).toFixed(1)}s`;
|
return `${round(ms / 1000, 1)}s`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${Math.round(ms / 1000)}s`;
|
const minutes = Math.floor(ms / 60000),
|
||||||
|
seconds = ms - minutes * 60000;
|
||||||
|
|
||||||
|
return `${minutes}m ${round(seconds / 1000, 1)}s`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatTimestamp(ts) {
|
function formatTimestamp(ts) {
|
||||||
|
|||||||
Reference in New Issue
Block a user