Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

ajax

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

like image 893
JSking Avatar asked Nov 03 '16 08:11

JSking


1 Answers

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
});
like image 143
Rory McCrossan Avatar answered Nov 03 '22 00:11

Rory McCrossan