JQuery-AJAX:表单发布超时和延迟后不再请求

发布于 2024-09-03 18:28:53 字数 1078 浏览 4 评论 0原文

我得到了一个包含多个复选框的表单。该表单应发送到服务器以从服务器端脚本接收适当的结果。

这已经在起作用了。

我现在要实现的目标:

1)实现超时:这已经可以工作,但是一旦发生超时,新的请求就不再工作。

2) 在请求结果中实现延迟:应实现延迟,以便并非每个复选框都会产生 POST 请求。

这就是我现在所拥有的:

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;

}

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

非常感谢您的建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

緦唸λ蓇 2024-09-10 18:28:53

对于延迟:

(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;

}
}());

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文