Ekumahost Ekumahost - 1 month ago 16
Ajax Question

How to add timeout to $.post in Jquery 3.x

$.post("result.php", {
name: vname,
email: vemail
}, function(response, status){
// Required Callback Function
});


I hav this code, and I just want to add a timeout of 10 seconds, should the user network is down I should alert "check ur internet connections". I do not want to use $.ajax

Answer

You can use a full $.ajax() call and provide the timeout property:

$.ajax({
    url: 'result.php',
    timeout: 10000,
    data: {
        name: vname,
        email: vemail
    },
    success: function(response) { 
        // Required Callback Function
    }
});

Alternatively you can use $.ajaxSetup() to affect all AJAX calls in the current scope:

$.ajaxSetup({
    timeout: 10000
});
$.post("result.php", { name: vname, email: vemail }, function(response, status) { 
    // Required Callback Function
});