检查Google Chrome中是否有资源可用

时间:2014-11-23 23:20:59

标签: google-chrome google-chrome-extension xmlhttprequest

我使用此代码了解我的会话将在我的大学到期的时间。

function checkRemaining() {
    var xhReq = new XMLHttpRequest();
    xhReq.open("GET", "http://192.168.168.168/dynUserLogin.html?loginDone=1", false);
    xhReq.send();

    var regex = /Login session time remaining: ([0-9]+) minutes/g;
    var res = regex.exec(xhReq.responseText);

    return res[1];
}

然而,当我没有连接到我大学的互联网时会产生错误。

在搜索此错误时,大多数人都使用了它,因为他们使用的是AdBlock 但在我的情况下(我猜其他人也有),有时资源可能无法使用。如果请求失败,我该如何触发函数?

chrome status failed

1 个答案:

答案 0 :(得分:1)

XMLHttpRequets将tell you what the result was

if (xhReq.status === 200) {
    // perfect!
} else {
    // there was a problem with the request,
    // for example the response may contain a 404 (Not Found)
    // or 500 (Internal Server Error) response code
}
相关问题