我有以下功能:
function mark_unmark_user_answer(targ, answer, answer_id, test_id, test_type, question_no, module_url) {
if(checked==targ){
targ.checked=false;
checked=false;
} else {
checked=targ;
}
$.post(module_url, {'test_id':test_id, 'question_no':question_no, 'op':'mark_ans', 'test_type':test_type, 'answer_no':answer, 'answer_id':answer_id}, function(data) {
if(jQuery.trim(data)=='unmark_ans') {
$('input[type="radio"]').removeAttr('checked');
$('#display_'+question_no).removeClass('green');
$('#display_'+question_no).removeClass('blue');
$('#display_'+question_no).addClass('orange');
} else {
//$('#mark_review').val('Mark');
$('#display_'+question_no).removeClass('orange');
$('#display_'+question_no).removeClass('blue');
$('#display_'+question_no).addClass("green");
$('#mark_review').attr('disabled', false);
}
var total_questions = $('#total_questions').val();
test_question_attempted_count( total_questions );
});
}
我想为此功能指定 30秒的超时时间。因此,如果在30秒内未收到对ajax请求的响应,则应显示警告消息“您的Internet连接有问题”。否则正常功能应该执行。
有人可以帮忙吗?
提前致谢。
答案 0 :(得分:5)
尝试使用
$.ajax({
type: "POST",
url: your_url_request,
data: {field: value, field_2: value_2},
timeout: 1000,
error: function(jqXHR, textStatus, errorThrown) {
if(textStatus==="timeout") {
//do something on timeout
}
}});
您可以在以下位置获得更多信息: Google Search Console
答案 1 :(得分:4)
您可以像{/ p>这样的$.ajaxSetup
方法设置Ajax请求的默认值
function mark_unmark_user_answer(targ, answer, answer_id, test_id, test_type, question_no, module_url) {
if(checked==targ){
targ.checked=false;
checked=false;
} else {
checked=targ;
}
$.ajaxSetup({
type: 'POST',
timeout: 30000,
error: function(xhr) {
$('#display_error')
.html('Error: ' + xhr.status + ' ' + xhr.statusText);
}
})
$.post(module_url, {'test_id':test_id, 'question_no':question_no, 'op':'mark_ans', 'test_type':test_type, 'answer_no':answer, 'answer_id':answer_id}, function(data) {
if(jQuery.trim(data)=='unmark_ans') {
$('input[type="radio"]').removeAttr('checked');
$('#display_'+question_no).removeClass('green');
$('#display_'+question_no).removeClass('blue');
$('#display_'+question_no).addClass('orange');
} else {
//$('#mark_review').val('Mark');
$('#display_'+question_no).removeClass('orange');
$('#display_'+question_no).removeClass('blue');
$('#display_'+question_no).addClass("green");
$('#mark_review').attr('disabled', false);
}
var total_questions = $('#total_questions').val();
test_question_attempted_count( total_questions );
});
}
答案 2 :(得分:-1)
从jQuery 1.2开始,您可以通过PlainObject向jQuery.post提供所有参数。
因此,您的代码段可以像这样重写:
$.post({ url: module_url, data: { … })
答案 3 :(得分:-4)
jQtimeout
60000
毫秒,用120000
秒内的120
替换您的时间({1}}秒var jQtimeout = 120000;
看起来像这个与Shivesh Chandra一起享受:)