最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Quickest way to check internet connectivity in JS - Stack Overflow

programmeradmin9浏览0评论

I have this code in a html file that checks for connectivity, but the problem here is that it takes about 10 seconds to throw the alert message box to indicate the connection is lost. I want to know if there's a much faster way to inform the user that the connection is lost without having to wait. Strictly JS thanks...

JS code:

<script language="JavaScript">
function SubmitButton()
{
    if(navigator.onLine)
    {
            document.getElementById('SubmitBtn').style.visibility='visible';
    }
    else
    {
        document.getElementById('SubmitBtn').style.visibility='hidden';
    }
}
function Online() 
{ 
    var status=false;
    status= navigator.onLine;
    if(status!= true)
    {
        alert('Network connectivity is lost, please try again later');
    }
}
</script>

Calling it in html file here:

<INPUT name="ccMCA1input" type="checkbox" onclick="ccMCA1Evaluate(),Online()" value=False>

I have this code in a html file that checks for connectivity, but the problem here is that it takes about 10 seconds to throw the alert message box to indicate the connection is lost. I want to know if there's a much faster way to inform the user that the connection is lost without having to wait. Strictly JS thanks...

JS code:

<script language="JavaScript">
function SubmitButton()
{
    if(navigator.onLine)
    {
            document.getElementById('SubmitBtn').style.visibility='visible';
    }
    else
    {
        document.getElementById('SubmitBtn').style.visibility='hidden';
    }
}
function Online() 
{ 
    var status=false;
    status= navigator.onLine;
    if(status!= true)
    {
        alert('Network connectivity is lost, please try again later');
    }
}
</script>

Calling it in html file here:

<INPUT name="ccMCA1input" type="checkbox" onclick="ccMCA1.ccEvaluate(),Online()" value=False>
Share Improve this question asked May 3, 2012 at 7:20 gaganHRgaganHR 3372 gold badges8 silver badges19 bronze badges 1
  • Is this way faster? stackoverflow.com/a/10249744/851498 – Florian Margaine Commented May 3, 2012 at 7:36
Add a comment  | 

3 Answers 3

Reset to default 7

You could periodically request a 1x1 gif image from a(ny) server, making sure you use the cache buster method to avoid caching. You could use the onload and onerror events.

var isOnline = (function isOnline(){
  // Create a closure to enclose some repeating data
  var state = false;
  var src = 'gif_url?t=' + Date.now();
  var function onLoad = function(){state = true;}
  var function onError = function(){state = false;}

  // Inside the closure, we create our monitor
  var timer = setInterval(function(){
    var img = new Image();
    img.onload = onLoad;
    img.onerror = onError;
    img.src = src;
  }, 10000);

  // Return a function that when called, returns the state in the closure
  return function(){return state;};
}());

//Use as
var amIOnline = isOnline();

navigator.onLine is the only built-in property which can be checked (and not reliable btw).
You could create a XHR request to a reliable server, and check whether any response is being received.

Consider checking out the following URLs:

  1. Online connectivity monitoring
  2. navigator.onLine testing
  3. on/offline event capture
发布评论

评论列表(0)

  1. 暂无评论