How to use timeout attribute with post method in jQuery ajax request?

I have the following function:

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

I want to set a timeout of 30 seconds for this function. Therefore, if the response to the ajax request is not received within 30 seconds, then a warning message will appear with the message that "your Internet connection has some problems." Otherwise, a normal function must be performed.

Can anyone help with this?

Thanks in advance.

+4
source share
4 answers

You can set default values ​​for an Ajax request in a method $.ajaxSetuplike this

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
source

$.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
        } 
    }});

You can have more information at: http://api.jquery.com/jQuery.ajax/

0
source
  • Just open the jquery.js file .
  • Now find jQtimeout
  • The default time is 60000Milisecond, replace with your time in Milisecond 120000in 120seconds
  • var jQtimeout = 120000; See how it is.
  • Done.

Enjoy Shiveh Chandra :)

-3
source

Source: https://habr.com/ru/post/1526867/


All Articles