每分钟从 Service Worker 调用一个函数,用于离线 PWA [英] Call a function every minute from a service worker for an offline PWA
本文介绍了每分钟从 Service Worker 调用一个函数,用于离线 PWA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发具有离线支持的渐进式 Web 应用 (PWA),我需要每分钟从 Service Worker 调用应用中的一个函数.(如果用户离线,则发送基于 Web API 的推送通知)
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)
最好的方法是什么?
推荐答案
每分钟调用一次函数使用 setInterval():
to call the function every minute use setInterval():
function myFunction(){
console.log('called evry minute')
}
setInterval(myFunction, 1000);
但是您可以监听 online
和 offline
事件以相应地发送通知,请参阅 NavigatorOnLine ,因为它在 Opera
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);
这篇关于每分钟从 Service Worker 调用一个函数,用于离线 PWA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文