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

255
Views
How to detect if internet connection is online again and alert something

I am using navigator.online to check if internet is offline or online (I know its downside but still its relevant to my project), how can I alert a message "Internet is back" and perform some actions as soon as internt comes again

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could use the online or offline event. Resource

window.addEventListener("online", () => {});
window.addEventListener("offline", () => {});
about 4 years ago · Juan Pablo Isaza Report

0

You can refer to this solution, converted to JS (original post link :https://stackoverflow.com/a/70737477/6696948)

    window.isDisconnected = false;
    
    function setIsDisconnected(val) {
      window.isDisconnected = val;
      document.body.textContent = !window.isDisconnected ? "online" : "offline"
    }
    function init() {
      handleConnectionChange();
      window.addEventListener("online", handleConnectionChange);
      window.addEventListener("offline", handleConnectionChange);
      function handleConnectionChange() {
        const condition = navigator.onLine ? "online" : "offline";
        if (condition === "online") {
          const webPing = setInterval(() => {
            fetch("https://jsonplaceholder.typicode.com/todos/1", {
              mode: "no-cors"
            })
              .then(() => {
                setIsDisconnected(false);
                clearInterval(webPing);
              })
              .catch(() => setIsDisconnected(true));
          }, 2000);
          return;
        }
        setIsDisconnected(true);
      }
    };
 
    init()

about 4 years ago · Juan Pablo Isaza Report
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!