I’d like to only have the service worker update check occur eg 5 mins into the app being used.
Problem is that because it’s wrapped in
window.addEventListener('load', async () => {
navigator.serviceWorker.register("/serviceWorker.js");
const registration = await navigator.serviceWorker.getRegistration();
it starts its checks at load time.
To get around it I put the service worker init in a function that I can call whenever I want, but of course, it fails because…
window.addEventListener('load', async ()
was called after the window was loaded.
What’s the general idea for delaying service worker checks?
>Solution :
You’ll want to use setTimeout() like this:
window.addEventListener('load', async () => {
navigator.serviceWorker.register("/serviceWorker.js");
setTimeout(async () => {
const registration = await navigator.serviceWorker.getRegistration();
}, 300000);
}
The first parameter to setTimeout() is a callback that is called after the delay, and the second parameter is that delay in milliseconds (5 minutes = 300,000 milliseconds).