jquery - 如何在 jQuery ajax 请求中使用 post 方法的超时属性?

标签 jquery ajax timeout

我有以下功能:

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/

相关文章:

javascript - jQuery 对话框 - X 按钮未显示

javascript - Cloudinary如何在php中生成签名

Haskell——Rand monad 中的计算超时

jQuery UI 模态对话框条纹问题

javascript - 将子菜单指示器添加到 li 项目纯 JavaScript

javascript - 如何在浏览器窗口最小化和服务器发出通知时播放通知提示音

php - 为dataTable jquery插件获取数组时出现"Illegal string offset"错误

javascript - AJAX 拖放

reactjs - 如何在 react 功能组件中进行超时然后清除超时?

c - 使用 gprof 分析非终止 C 程序