I have some code in my serviceworker that checks if a website is online:
var check = false;
setInterval(checkOnline, 60000);
This code checks every minute in the background if the website is online. This code:
function checkOnline() {
fetch(www.example.com)
.then(response => {
})
.catch(error => {
const title = 'hhh';
const options = {
body: 'hhh',
icon: 'images/icon.png',
badge: 'images/badge.png'
};
self.registration.showNotification(title, options)
check = true;
});
}
The problem is When it sets the boolean on true in this function, the serviceworker runs the page again and the boolean is set on false by default, but I need it to stay true so it only does something one time, and not the whole time the code goes to the catch. How do I solve this? I just want that if the website is offline it only shows the notification one time. But it keeps running the code so the notification will come every minute if the website is offline. All this code is on my serviceworker because it needs to be checked even if the browser is not open. This is an PWA application.