11

I've a 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 assign time-out of 30 seconds to this function. So if the response for the ajax request is not received within 30 seconds then the alert message saying that "Your internet connection has some problem" should appear. Otherwise normal function should get execute.

Can anyone help on this?

Thanks in advance.

Jalpesh Vadgama
  • 13,653
  • 19
  • 72
  • 94
PHPLover
  • 1
  • 51
  • 158
  • 311

4 Answers4

10

Try use

$.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 has more information in: http://api.jquery.com/jQuery.ajax/

Santos L. Victor
  • 628
  • 8
  • 13
6

You can set defaults for Ajax request in $.ajaxSetup method like 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 );    
});
}
bart
  • 14,958
  • 21
  • 75
  • 105
DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
-4

Since jQuery 1.2 you are able to provide all parameters to jQuery.post via PlainObject.

So, your snippet can be rewritten like this:

$.post({ url: module_url, data: { … })
-10
  1. Just Open jquery.js File.
  2. Now Find jQtimeout
  3. Default Set Time is 60000 Milisecond, Replace with Your Time In Milisecond 120000 for 120 Seconds
  4. var jQtimeout = 120000; Look like This
  5. It's Done.

Enjoy with Shivesh Chandra :)

Shivesh96
  • 1
  • 3