Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!