在 Service Worker 中使用 setInterval [英] Using setInterval in Service worker

查看:74
本文介绍了在 Service Worker 中使用 setInterval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请我想使用 setInterval 它在我的服务工作者 javascript 文件中每 5 秒显示一次通知,它起初工作正常,但在大约 20 秒后逐渐停止工作

这里是service worker java脚本文件中timer初始化的部分

self.addEventListener('activate', function(event) {设置间隔(函数(){self.registration.showNotification("title", {正文:测试"});},5000);});

解决方案

Service Worker 的生命周期是有限的,你不能让他们永远活着.

请参阅 Service Workers 规范的生命周期段落..>

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

});

解决方案

Service workers have a limited lifetime, you can't keep them alive forever.

See the lifetime paragraph of the Service Workers specification.

这篇关于在 Service Worker 中使用 setInterval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆