我使用jquery进行了ajax调用。
$.ajax({
type: "POST",
url: "recorded-vigil.aspx/GetCameraTimeLineMinutes",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: data,
error: function (xhr) {
$('.loader').hide();
alert('Error in fetching timeline. Please try again');
console.log(xhr);
},
success: function (results) {
$('.loader').hide();
$('#spnTimeSelect').html("Please select minute");
console.log('GetCameraTimeLineMinutes');
if (results != null && results.d != null && results.d.result != null) {
var HourSel = $("#MinuteSelect").empty();
for (var i = 0; i < results.d.result.length; i++) {
var Minute = i;
//if (results.d.result[i] == 1)
// HourSel.append('<button class="dayClass activeCam minuteClass" data-minuteindex="' + Minute + '">' + Minute + '</button>');
//else
// HourSel.append('<button class="dayClass minuteClass" data-minuteindex="' + Minute + '">' + Minute + '</button>');
if (results.d.result[i] == 1)
HourSel.append('<div class="btn on minbutton minuteClass" data-minuteindex="' + Minute + '">' + Minute + '</div>');
else
HourSel.append('<div class="btn minbutton minuteClass" data-minuteindex="' + Minute + '">' + Minute + '</div>');
//if (i == 16)
// oSel.append('<hr style="visibility:hidden;"><div id="dayBreak"> </div>');
}
breakMinutes();
$('.minuteClass').on('click', onMinuteClick);
}
}
});
这通常由于服务器超时而引发错误。 有什么办法可以在错误时再次触发它。如果我们尝试说3次,则可以显示超时错误。有可能吗?
答案 0 :(得分:3)
如果我正确理解您的情况,那么可以。您可以通过多种方式实现此目标-考虑对代码进行以下调整,以显示如何实现此目标:
var retries = 3; // Track the number of retries
function doRequest() {
$.ajax({
type: "POST",
url: "recorded-vigil.aspx/GetCameraTimeLineMinutes",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: data,
error: function (xhr) {
$('.loader').hide();
alert('Error in fetching timeline. Please try again');
console.log(xhr);
retries--; // Decrement the number of remaining retries
if(retries >= 0) { // If more retries available, fire another request
doRequest()
}
},
success: function (results) {
retries = 3; // Optional reset if you want to do this also
$('.loader').hide();
$('#spnTimeSelect').html("Please select minute");
console.log('GetCameraTimeLineMinutes');
if (results != null && results.d != null && results.d.result != null) {
var HourSel = $("#MinuteSelect").empty();
for (var i = 0; i < results.d.result.length; i++) {
var Minute = i;
//if (results.d.result[i] == 1)
// HourSel.append('<button class="dayClass activeCam minuteClass" data-minuteindex="' + Minute + '">' + Minute + '</button>');
//else
// HourSel.append('<button class="dayClass minuteClass" data-minuteindex="' + Minute + '">' + Minute + '</button>');
if (results.d.result[i] == 1)
HourSel.append('<div class="btn on minbutton minuteClass" data-minuteindex="' + Minute + '">' + Minute + '</div>');
else
HourSel.append('<div class="btn minbutton minuteClass" data-minuteindex="' + Minute + '">' + Minute + '</div>');
//if (i == 16)
// oSel.append('<hr style="visibility:hidden;"><div id="dayBreak"> </div>');
}
breakMinutes();
$('.minuteClass').on('click', onMinuteClick);
}
}
});
}