How can we emulate the timeout of $.ajax using $.post?
$.POST is a preset version of $.ajax, so few parameter are already set.
As a matter of fact, a
$.postis equal to$.ajax({ type: 'POST', url: url, data: data, success: success, dataType: dataType });
But, you can create your own post function to send the request through $.ajax at last.
Here is a custom POST plugin I just coded.
(function( $ ){
$.myPOST = function( url, data, success, timeout ) {
var settings = {
type : "POST", //predefine request type to POST
'url' : url,
'data' : data,
'success' : success,
'timeout' : timeout
};
$.ajax(settings)
};
})( jQuery );
Now the custom POST function is ready
Usage:
$.myPOST(
"test.php",
{
'data' : 'value'
},
function(data) { },
5000 // this is the timeout
);
Enjoy :)
-
Will this not spawn multiple calls to the script and cause a new dataset to be generated with each call? – Anriëtte Myburgh Nov 30 '17 at 10:52
$.postinstead of$.ajax? If you must, you can set a global timeout for all requests using$.ajaxSetup({'timeout': ...}). – pjumble Apr 4 '12 at 10:40