我有以下功能:
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 请求的响应,则会出现警告消息“您的互联网连接有问题”。否则正常的函数应该得到执行。
有人可以帮忙吗?
提前致谢。
最佳答案
尝试使用
$.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
}
}});
您可以在以下位置获取更多信息: http://api.jquery.com/jQuery.ajax/
关于jquery - 如何在 jQuery ajax 请求中使用 post 方法的超时属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21771399/