Set Timeout For Ajax (Jquery)

Set timeout for ajax (jQuery)

Please read the $.ajax documentation, this is a covered topic.

$.ajax({
url: "test.html",
error: function(){
// will fire when timeout is reached
},
success: function(){
//do something
},
timeout: 3000 // sets timeout to 3 seconds
});

You can get see what type of error was thrown by accessing the textStatus parameter of the error: function(jqXHR, textStatus, errorThrown) option. The options are "timeout", "error", "abort", and "parsererror".

Jquery/ajax Looping SetTimeout

Put the code in a function and call it in the success or complete handler:

function load() {
setTimeout(function () {
$.ajax({
url: "includes/handlechat.php",
type: "GET",
data: data,
dataType: 'json',
success: function (result) {
$("#clog").empty();
$.each(result, function (rowKey, row) {
$("#clog").append('<p ><h4>' + row.username + ':</h4>' + row.message_content + '</p>');
});
},
complete: load
});
}, 1101);
}
load();

You can also use an IIFE to avoid creating another binding in the current environment:

(function load() {
// setTimeout here
}());

jQuery ajax call set timeout

If you wrap this in a setInterval, you run the risk of sending your server a ton of requests with the potential of not getting them all back in order. Put a timeout inside the callback:

function runAjax() {
$.ajax({
url: "data.php"
}).done(function(data) {
//code
setTimeout(runAjax, 2000); //Run it again in 2 sec
});
}

runAjax(); //I suppose we should start the AJAX :D

Jquery ajax setTimeout after form succes not redirecting?

String.replace() requires two parameters. As written, it will look for the string "index.php" and replace it with nothing. Try adding a regex to match everything and replace with your new URL.

setTimeout(function() {
window.location.replace( /.*/, "index.php");
},1000);

Use the full URL (i.e. https://yourdomain.com/index.php) or write a better regex. For instance, if your domain ends with .com, you could do something like:

setTimeout(function() {
window.location.replace( /\.com\/.*/, ".com/index.php");
},1000);

Timeout in function with ajax jquery

Docs

Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.

timeout in $.ajax() sSet a timeout (in milliseconds) for the request to complete, if for any reason the request is not completed with in the time frame the request will abort

You have to use

setInterval(function() {      
$.ajax({
url: '../pujar',
dataType: 'json',
type: 'get',
cache: true,
success: function (data) {
}
});
}, 1000);


Related Topics



Leave a reply



Submit