我正在尝试执行递归的AJAX调用来处理我的应用程序中的一个功能。方案如下:
我想确保如果服务器将String B作为响应返回超过两分钟,则应停止递归执行并向用户显示错误消息。
有人可以提出适当的方法吗?
以下是JS代码示例。我们正在使用ExtJS:
function process()
{
Ext.Ajax.request({
url : "ABC.action",
form: 'formname',
params:{
method : "Serversidehandler",
},
success : processSuccess,
failure : processFail
});
}
function processSuccess(request)
{
if(request.responseText != null)
{
var responseJSON = Ext.decode(request.responseText);
var response = responseJSON.response;
if(response != null)
{
if(response == "A")
{
// do something
}
else if(response == "B")
{
process() // main method called again
}
}
}
}
答案 0 :(得分:1)
尝试下面给出的代码,setTimeout可以正常使用延迟。
请更清楚地说明要求。
function process()
{
Ext.Ajax.request({
url : "ABC.action",
form: 'formname',
params:{
method : "Serversidehandler",
},
success : processSuccess,
failure : processFail
});
}
function processSuccess(request)
{
if(request.responseText != null)
{
var responseJSON = Ext.decode(request.responseText);
var response = responseJSON.response;
if(response != null)
{
if(response == "A")
{
// do something
}
else if(response == "B")
{
window.setTimeout(process(), 2000); // main method called again
}
}
}
}
答案 1 :(得分:0)
var startTime = (new Date).getTime();
function process() {
Ext.Ajax.request({
url: "ABC.action",
form: 'formname',
params: {
method: "Serversidehandler",
},
success: processSuccess,
failure: processFail
});
}
function processSuccess(request) {
var elapsedTime = 0;
if (request.responseText != null) {
var responseJSON = Ext.decode(request.responseText);
var response = responseJSON.response;
if (response != null) {
if (response == "A") {
// do something
}
else if (response == "B") {
elapsedTime = (new Date().getTime()) - startTime;
if (elapsedTime <= 2000) {
process() // main method called again
};
}
}
}
}