timer tweaks

Signed-off-by: Andy Miller <rhuk@mac.com>
This commit is contained in:
Andy Miller
2025-10-19 11:29:25 -06:00
parent ce38f72600
commit 49a9b3bcc9
2 changed files with 72 additions and 14 deletions

View File

@@ -37,7 +37,9 @@ const NICETIME_PERIODS_LONG = [
];
const NICETIME_LENGTHS = [60, 60, 24, 7, 4.35, 12, 10];
const NICETIME_REFRESH_INTERVAL = 15000;
const FAST_UPDATE_THRESHOLD_SECONDS = 60;
const FAST_REFRESH_INTERVAL_MS = 1000;
const SLOW_REFRESH_INTERVAL_MS = 60000;
const NICETIME_TRANSLATION_ROOTS = ['GRAV_CORE', 'GRAV'];
const NICETIME_BASE_FALLBACKS = {
@@ -234,23 +236,56 @@ const initialiseNicetimeUpdater = () => {
}
const update = () => {
const nowSeconds = Math.floor(Date.now() / 1000);
let youngestAge = Infinity;
document.querySelectorAll(selector).forEach((element) => {
const timestamp = element.getAttribute('data-nicetime-timestamp');
const longStrings = parseBoolAttribute(element, 'data-nicetime-long', false);
const showTense = parseBoolAttribute(element, 'data-nicetime-tense', false);
const unixTimestamp = parseTimestampValue(timestamp);
if (unixTimestamp !== null) {
const age = Math.max(0, nowSeconds - unixTimestamp);
if (age < youngestAge) {
youngestAge = age;
}
}
const updated = computeNicetime(timestamp, { longStrings, showTense });
if (updated && element.textContent !== updated) {
element.textContent = updated;
}
});
return youngestAge;
};
update();
const intervalId = window.setInterval(update, NICETIME_REFRESH_INTERVAL);
window.addEventListener('beforeunload', () => window.clearInterval(intervalId), { once: true });
let timerId = null;
return { update, destroy: () => window.clearInterval(intervalId) };
const scheduleNext = (lastAge) => {
const useFastInterval = Number.isFinite(lastAge) && lastAge < FAST_UPDATE_THRESHOLD_SECONDS;
const delay = useFastInterval ? FAST_REFRESH_INTERVAL_MS : SLOW_REFRESH_INTERVAL_MS;
timerId = window.setTimeout(() => {
const nextAge = update();
scheduleNext(nextAge);
}, delay);
};
const destroy = () => {
if (timerId !== null) {
window.clearTimeout(timerId);
timerId = null;
}
};
const initialAge = update();
scheduleNext(initialAge);
window.addEventListener('beforeunload', destroy, { once: true });
return { update, destroy };
};
class RestoreManager {

View File

@@ -10986,7 +10986,9 @@ var urls = {
var NICETIME_PERIODS_SHORT = ['NICETIME.SEC', 'NICETIME.MIN', 'NICETIME.HR', 'NICETIME.DAY', 'NICETIME.WK', 'NICETIME.MO', 'NICETIME.YR', 'NICETIME.DEC'];
var NICETIME_PERIODS_LONG = ['NICETIME.SECOND', 'NICETIME.MINUTE', 'NICETIME.HOUR', 'NICETIME.DAY', 'NICETIME.WEEK', 'NICETIME.MONTH', 'NICETIME.YEAR', 'NICETIME.DECADE'];
var NICETIME_LENGTHS = [60, 60, 24, 7, 4.35, 12, 10];
var NICETIME_REFRESH_INTERVAL = 15000;
var FAST_UPDATE_THRESHOLD_SECONDS = 60;
var FAST_REFRESH_INTERVAL_MS = 1000;
var SLOW_REFRESH_INTERVAL_MS = 60000;
var NICETIME_TRANSLATION_ROOTS = ['GRAV_CORE', 'GRAV'];
var NICETIME_BASE_FALLBACKS = {
SECOND: 'second',
@@ -11157,10 +11159,19 @@ var initialiseNicetimeUpdater = function initialiseNicetimeUpdater() {
return null;
}
var update = function update() {
var nowSeconds = Math.floor(Date.now() / 1000);
var youngestAge = Infinity;
document.querySelectorAll(selector).forEach(function (element) {
var timestamp = element.getAttribute('data-nicetime-timestamp');
var longStrings = parseBoolAttribute(element, 'data-nicetime-long', false);
var showTense = parseBoolAttribute(element, 'data-nicetime-tense', false);
var unixTimestamp = parseTimestampValue(timestamp);
if (unixTimestamp !== null) {
var age = Math.max(0, nowSeconds - unixTimestamp);
if (age < youngestAge) {
youngestAge = age;
}
}
var updated = computeNicetime(timestamp, {
longStrings: longStrings,
showTense: showTense
@@ -11169,19 +11180,31 @@ var initialiseNicetimeUpdater = function initialiseNicetimeUpdater() {
element.textContent = updated;
}
});
return youngestAge;
};
update();
var intervalId = window.setInterval(update, NICETIME_REFRESH_INTERVAL);
window.addEventListener('beforeunload', function () {
return window.clearInterval(intervalId);
}, {
var timerId = null;
var _scheduleNext = function scheduleNext(lastAge) {
var useFastInterval = Number.isFinite(lastAge) && lastAge < FAST_UPDATE_THRESHOLD_SECONDS;
var delay = useFastInterval ? FAST_REFRESH_INTERVAL_MS : SLOW_REFRESH_INTERVAL_MS;
timerId = window.setTimeout(function () {
var nextAge = update();
_scheduleNext(nextAge);
}, delay);
};
var destroy = function destroy() {
if (timerId !== null) {
window.clearTimeout(timerId);
timerId = null;
}
};
var initialAge = update();
_scheduleNext(initialAge);
window.addEventListener('beforeunload', destroy, {
once: true
});
return {
update: update,
destroy: function destroy() {
return window.clearInterval(intervalId);
}
destroy: destroy
};
};
var RestoreManager = /*#__PURE__*/function () {