JQuery-AJAX: No further request after timeout and delay in form post
I got a form containing multiple checkboxes. This form shall be sent to the server to receive appropriate results from a server side script.
This is already working.
What I would achieve now:
1) Implementing a timeout: This is already working, but as soon as a timeout occurs, a new request is not working anymore.
2) Implementing a delay in requesting results: A delay shall be implemented so that not every checkbox is resulting in a POST request.
This is what I have right now:
function update_listing() {
// remove postings from table
$('.tbl tbody').children('tr').remove();
// get the results through AJAX
$.ajax({
type: "POST",
url: "http://localhost/hr/index.php/listing/ajax_csv",
data: $("#listing_form").serialize(),
timeout: 5000,
success: function(data) {
$(".tbl tbody").append(data);
},
error: function(objAJAXRequest, strError) {
$(".tbl tbody").append("<tr><td>failed " + strError + "</td></tr&g开发者_如何学Got;");
}
});
return true;
}
Results are for now passed as HTML table rows - I will transform them to CSV/JSON in the next step.
Thanks so much for your advice.
For the delay:
(function () {
var timeout;
function update_listing() {
// remove postings from table
clearTimeout(timeout);
timeout = setTimeout(function () {
$('.tbl tbody').children('tr').remove();
// get the results through AJAX
$.ajax({
type: "POST",
url: "http://localhost/hr/index.php/listing/ajax_csv",
data: $("#listing_form").serialize(),
timeout: 5000,
success: function(data) {
$(".tbl tbody").append(data);
},
error: function(objAJAXRequest, strError) {
$(".tbl tbody").append("<tr><td>failed " + strError + "</td></tr>");
}
});
}, 1000); // 1 second?
return true;
}
}());
This will wait a second until making the AJAX request. What do you mean with regards to "as soon as a timeout occurs, a new request is not working anymore.". If you want to trigger another request if one fails, just call update_list()
again (but note that the 1-second delay will be in effect).
精彩评论