Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
serviceworker in reactjs PWA, needs to stop running a function when boolean is true, but keeps resetting the boolean

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.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda