Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the proper way to delay a service worker update check?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading