好吧所以我想要的是在按下按钮后开始循环ajax调用,ajax将与数据库进行通信,并根据响应,我希望它循环,并重新执行完全相同的调用,或者停止呼叫并转到另一个功能。
我现在有以下代码:
function checkResult(session, ip, key){
$.ajax({
url: "myUrl"+key,
}).success(function(returnData){
returnData = returnData.replace("\n", "");
returnData = returnData.replace(" ", "");
if(returnData == 1){//Everything is fine, stop the call
clearInterval(interval);
console.log("completed");
}else if(returnData == 2){//Something went wrong, stop the call
clearInterval(interval);
console.log("Something went wrong");
}else if(returnData == 0){//No result found, repeat call
console.log("Nothing was found");
return returnData;
}
});
}
function repeatCall(session, ip, key){
console.log("Repeating the request");
interval = setInterval(function(){ var status = checkPayment(session, ip, key); if(status != 0){
clearInterval(interval);
checkResult(status)}}, 1000);
}
它目前仅循环整个调用一次,它在我的控制台中记录“未找到任何内容”。让这个工作的正确方法是什么?
答案 0 :(得分:0)
呃,如果我理解你的话,你应该可以做这样的事情......
function makeAJAXRequest(session, ip, key)
{
$.ajax({
type:'POST', // or GET
url: "myUrl"+key,
success: function(status){
returnData = returnData.replace("\n", "");
returnData = returnData.replace(" ", "");
if(returnData == 1)
{ //Everything is fine, stop the call
clearInterval(interval);
console.log("completed");
}
else if(returnData == 2)
{ //Something went wrong, stop the call
clearInterval(interval);
console.log("Something went wrong");
}
else if(returnData == 0)
{ //No result found, repeat call
console.log("Nothing was found");
makeAJAXRequest(session, ip, key);
}
},
error: function(status, err){
// dome something
}
});
}