[英]How to show only the last ajax request result
总计ajax请求的数量,并在它是最后一个ajax响应时显示ajax响应。
换句话说,如何仅使用上次发送的ajax请求的响应。
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/
}
});
}
如果用户在显示响应之前单击了几个线程,我将在查看消息详细信息时使用它,它将在显示当前所选线程详细信息之前显示先前的线程详细信息
任何更好的解决方案都适合共享
您应该在发送请求后立即递减。
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;
}
}
});
}
您或多或少都在正确的路线上。 这是避免先前调用回叫的唯一方法。 您将必须将一个ID与每个请求相关联,然后检查该请求的ID是否为最后发送的请求。
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);
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.