2

Total up the amount of the ajax request and show the ajax response when it is the last ajax response.

In other words, how to use only the response of last sent ajax request.

var req=0;
function ajaxReq(){
    req++; /total up the amount of request/
    $.ajax({
        url: "result.php",
        type: 'GET',
        contentType: false,
        enctype: 'multipart/form-data',
        cache: false,
        processData: false,
        success: function(response) {
            if(req==1){ /show the response when this is the last request/
                $("#response-element").html(response);
            }
            req--; /Subtract when every success ajax response/
        }
    });
}

I using this on viewing messages detail if the user clicked few threads before the response show out it will show the previous thread detail before the current selected thread detail shown out

Any better solution would be nice for sharing

2
  • Hey Leo, can you help us understand your use case? If you only want the last response, what is the purpose behind making the previous ajax calls?
    – tjfo
    Commented Feb 10, 2017 at 3:24
  • I already updated the question
    – Leo
    Commented Feb 10, 2017 at 3:52

2 Answers 2

3

You should decrement as soon as you send request.

var req=""; // should be a number
function ajaxReq(){
    req++; /total up the amount of request/
    $.ajax({
        url: "result.php",
        type: 'GET',
        contentType: false,
        enctype: 'multipart/form-data',
        cache: false,
        processData: false,
        beforeSend: function() {
            if (req > 1) req -= 1;
        },
        success: function(response) {
            if(req==1){ /show the response when this is the last request/
                $("#response-element").html(response);
                req -= 1;
            }
        }
    });
}
3
  • Thanks :D was wondering if there is any better solution for this :D will improve it
    – Leo
    Commented Feb 10, 2017 at 3:40
  • There is a cleaner way using async.js but that would be overkill. Commented Feb 10, 2017 at 3:44
  • Hi, I had tried yours, but the response is not showing..I think is because of the decrement after sending request, the amount would be 0 so it's not gonna show the response
    – Leo
    Commented Feb 10, 2017 at 3:45
1

You are more or less on the right lines. This is the only way you can avoid the callbacks of previous calls. You will have to associate an ID with each request and then check if the ID of the request is the last sent request.

var sentReqCount = 0;

setInterval(function() {
    sentReqCount++;
    ajaxReq(sentReqCount);
}, 100);

function ajaxReq(thisReqId) {
    $.ajax({
        url: "result.php",
        type: 'GET',
        contentType: false,
        enctype: 'multipart/form-data',
        cache: false,
        processData: false,
        success: function(response) {
            if (thisReqId === sentReqCount) {
                $("#response-element").html(response);
            }
        }
    });
}
4
  • by this way, can you pass parameter to the function when onclick event??
    – Leo
    Commented Feb 10, 2017 at 4:03
  • example , pass the parameter like onClick="viewTrip('id','value')", the id and value needed to get the messages
    – Leo
    Commented Feb 10, 2017 at 4:04
  • You can! But how is it related to this question?
    – kvn
    Commented Feb 10, 2017 at 4:24
  • Yes yes, It could be much helps
    – Leo
    Commented Feb 10, 2017 at 5:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.