diff --git a/static/js/chat.js b/static/js/chat.js index a48753e..9e7e6d3 100644 --- a/static/js/chat.js +++ b/static/js/chat.js @@ -261,7 +261,7 @@ this.#_time = make("div", "time"); if (this.#time) { - this.#_time.textContent = `${this.#time.toFixed(1)}s`; + this.#_time.textContent = formatMilliseconds(this.#time * 1000); } _body.appendChild(this.#_time); @@ -868,7 +868,7 @@ this.#time = time; if (this.#time) { - this.#_time.textContent = `${this.#time.toFixed(1)}s`; + this.#_time.textContent = formatMilliseconds(this.#time * 1000); } } diff --git a/static/js/lib.js b/static/js/lib.js index 989f06f..ec94bf0 100644 --- a/static/js/lib.js +++ b/static/js/lib.js @@ -46,16 +46,25 @@ function escapeHtml(text) { return text.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) { if (ms < 1000) { return `${ms}ms`; } - if (ms < 10000) { - return `${(ms / 1000).toFixed(1)}s`; + if (ms < 60000) { + 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) {