setinterval in service worker

2,670 views
Skip to first unread message

alaa hamed

unread,
May 6, 2016, 4:29:03 PM5/6/16
to service-worker-discuss

Please I want to use setInterval which shows a notification every 5 seconds inside my service worker javascript file and it works fine at first but gradually after about 20 seconds it stops working




Here is the part where timer is initialized in the service worker java script file











self.addEventListener('activate', function(event) {








  setInterval
(function()








   
{








     
self.registration.showNotification("title", {








       body
: "test"








     
});








   
},5000);






















});


Matt Falkenhagen

unread,
May 9, 2016, 9:55:59 PM5/9/16
to service-worker-discuss
+service-worker-discuss this time.

2016-05-09 10:50 GMT+09:00 Matt Falkenhagen <fal...@chromium.org>:
The browser may terminate the service worker at any time. Basically you can't use setTimeout reliably in a service worker. The supported way to keep a service worker alive is passing a promise to waitUntil/respondWith, but even then the browser may time out the promise and terminate the worker.


--
You received this message because you are subscribed to the Google Groups "service-worker-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to service-worker-di...@chromium.org.
To post to this group, send email to service-wor...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/service-worker-discuss/0532249f-85fd-42d7-8219-b9f773ac4cce%40chromium.org.


Salvador de la Puente González

unread,
Jun 6, 2016, 10:55:18 AM6/6/16
to Matt Falkenhagen, service-worker-discuss
That's the expected behaviour. Run time execution for Service Workers is limited, so the script is only kept alive for short periods of time. This time could be extended with `evt.waitUntil()` but even using this mechanism, browsers would detect if you are abusing the functionality (and, in this case, you definitively are) so it will be eventually killed. Remember the service worker is not a background process.

Reply all
Reply to author
Forward
0 new messages