今天看到jason的一段代码用来给所有Ajax.Request设置超时时间,
但如果想给每个Ajax.request设置不同的超时间呢?我改了改(黑体部分)如下:
function callInProgress (xmlhttp) {
switch (xmlhttp.readyState) {
case 1: case 2: case 3:
return true;
break;
// Case 4 and 0
default:
return false;
break;
}
}
function showFailureMessage() {
alert('uh oh, it looks like the network is down. Try again shortly');
}
// Register global responders that will occur on all AJAX requests
Ajax.Responders.register({
onCreate: function(request) {
request['timeoutId'] = window.setTimeout(
function() {
// If we have hit the timeout and the AJAX request is active, abort it and let the user know
if (callInProgress(request.transport)) {
request.transport.abort();
showFailureMessage();
// Run the onFailure method if we set one up when creating the AJAX object
if (request.options['onFailure']) {
request.options['onFailure'](request.transport, request.json);
}
}
}, request.options.timeoutSeconds?request.options.timeoutSeconds*1000:30*1000
);
},
onComplete: function(request) {
// Clear the timeout, the request completed ok
window.clearTimeout(request['timeoutId']);
}
});
像下面这样调用:
new Ajax.Request(url, {
timeoutSeconds:5,
parameters:{ //code... },
onSuccess: function(transport) { //code... }
});
通常要想以后注销responder得这样:
var Responder = {
onCreate: function(request){//code...},
onComplete: function(request){//code...}
};
Ajax.Responders.register(Responder);
Ajax.Responders.unregister(Responder);
引用
阅读(3097) | 评论(0) | 转发(0) |