2014-02-14 40 views
4

我有一個如下功能:如何使用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請求的響應,則應該出現警告消息,指出「您的互聯網連接有問題」。否則,普通函數應該執行。

任何人都可以幫忙嗎?

在此先感謝。

+1

請讓我知道,如果我的回答對你起作用,或者如果你對答案有任何問題。請接受我的答覆作爲正確的答案,如果它對您有用 - 以便其他用戶可以從中受益:知道答案有效並將問題標記爲已答覆。 – DhruvJoshi

回答

2

可以爲Ajax請求的默認值在$.ajaxSetup方法這樣

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);  
}); 
} 
+0

使用ajaxSetup [不推薦](http://api.jquery.com/jQuery.ajaxSetup/)。 – texnic

-3
  1. Just Open jquery.js文件。
  2. 立即查找jQtimeout
  3. 默認設置時間爲60000 Milisecond,替換你的時間Milisecond 120000120
  4. var jQtimeout = 120000;這個樣子
  5. 它的完成。

享受Shivesh錢德拉:)

+1

...並重復每次更新jquery;) – aaron

0

嘗試使用

$.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/

相關問題