Last active
March 6, 2023 14:11
-
-
Save SeinopSys/306ff83b879cab927bdb48bf756c3b1e to your computer and use it in GitHub Desktop.
Reload a browser tab in the nearest X seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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