views:

203

answers:

1

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>");
                                }
                    });

    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.

+1  A: 

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

Matt
Thanks Matt, the timeout is working great.With regards to 1):As long as the (AJAX) timeout is not triggered, everything works as expected, but as soon as the request ist aborted all following post request are immediately aborted (no single one is executed anymore).
Nogga