JQuery-AJAX:超时后没有进一步的请求和表单发布的延迟

时间:2022-04-02 01:18:48

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.

1)实现超时:这已经有效,但是一旦发生超时,新请求就不再起作用了。

2) Implementing a delay in requesting results: A delay shall be implemented so that not every checkbox is resulting in a POST request.

2)实现请求结果的延迟:应实现延迟,以便不是每个复选框都产生POST请求。

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.

结果现在作为HTML表行传递 - 我将在下一步将它们转换为CSV / JSON。

Thanks so much for your advice.

非常感谢您的建议。

1 个解决方案

#1


2  

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

这将等待一秒钟,直到发出AJAX请求。对于“一旦超时发生,新请求不再起作用”,你的意思是什么?如果您想在另一个请求失败时触发另一个请求,只需再次调用update_list()(但请注意,1秒延迟将生效)。

#1


2  

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

这将等待一秒钟,直到发出AJAX请求。对于“一旦超时发生,新请求不再起作用”,你的意思是什么?如果您想在另一个请求失败时触发另一个请求,只需再次调用update_list()(但请注意,1秒延迟将生效)。