Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda