Call a function every minute from a service worker for an offline PWA [closed]

不问归期 提交于 2019-12-25 18:05:18

问题


I'm working on a Progressive Web App (PWA) with offline support and I need to call a function in the app every minute from the service worker. (to send a web API based push notification if the user is offline)

What is the best way to do this?


回答1:


to call the function every minute use setInterval():

function myFunction(){
	console.log('called evry minute')
}

setInterval(myFunction, 1000);

But you can listen for online and offline events to send the notifications accordingly , see compatibility of NavigatorOnLine , as it won't work in Opera

window.addEventListener('online',  functionWhenOnline);
window.addEventListener('offline', functionWhenOffline);


来源:https://stackoverflow.com/questions/49825813/call-a-function-every-minute-from-a-service-worker-for-an-offline-pwa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!