Skip to content

Instantly share code, notes, and snippets.

@SeinopSys
Last active March 6, 2023 14:11
Show Gist options
  • Save SeinopSys/306ff83b879cab927bdb48bf756c3b1e to your computer and use it in GitHub Desktop.
Save SeinopSys/306ff83b879cab927bdb48bf756c3b1e to your computer and use it in GitHub Desktop.
Reload a browser tab in the nearest X seconds
(function reloadInSeconds () {
const s = parseInt(prompt('Reload in nearest Nth second, where N is:'), 10);
if (isNaN(s)) return;
const ms = s * 1e3;
const now = Date.now();
const reloadAtTs = (Math.ceil(now / ms) + 1) * ms;
console.info('Reloading at', new Date(reloadAtTs).toLocaleString());
const countdownTick = () => {
const timeleft = reloadAtTs - Date.now();
const reloadInRounded = Math.round(timeleft/1e3);
if (reloadInRounded > 0) {
console.info(`Reloading in ${reloadInRounded}s`);
return;
}
clearInterval(countdownInterval);
};
const countdownInterval = setInterval(countdownTick, 1e3);
setTimeout(() => {
console.info('Reloading…');
window.location.reload();
}, reloadAtTs - now);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment