javascript ajax state #js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript ajax state #js相关的知识,希望对你有一定的参考价值。

//Since maybe you can start many AJAX requests, I'd argue that one of best solutions - basically, a reliable solution - is first creating an array of requests' state like this:

var ajaxRequestsState = [];
//When you start an AJAX request, you should add an element to this array like this one:

ajaxRequestsState.push({ requestId: "some identifier" });
//Later, if you need to check if there's no active request, you can have a flag like so:

function isAsyncRequestActive() {
     return ajaxRequestsState.length > 0;
}
//Finally, whenever a request ends or fails, you must do so:

function releaseRequest(requestId) {
    var requestIndex = 0;
    var found = false;

    while(!found && requestIndex < ajaxRequestsState.length)
    {
        found = ajaxRequestsState[requestIndex].requestId == requestId;
        requestIndex++;
    }

    if(found) {
        ajaxRequestsState.splice((requestIndex-1), 1);
    }
}

releaseRequest("identifier of request which ended or failed");
//That's simply tracking requests' state and maintaining a collection of requests' states, and you'll have it!

以上是关于javascript ajax state #js的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 和Ajax跨域问题

[javascript]原生js实现Ajax

JavaScript学习总结12JS AJAX应用

javascript Ajax JS

javascript 停ajax.js

javascript AJAX与Vanilla JS