jQuery的阿贾克斯循环 [英] Jquery ajax in loop

查看:139
本文介绍了jQuery的阿贾克斯循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个SQL转换器负责处理MySQL来的MongoDB。我写的界面给我的转换器的AJAX。

I am writing a Sql convertor which handles mysql to mongodb. I am writing an interface to my converter with ajax.

阿贾克斯处理巨大的convertions。这样就把限制到mysql选择code,使convertions 1000分手的时间。

Ajax is handling huge convertions. This put limit to mysql select code and make convertions parted by 1000 at a time.

我的code是:

$(document).ready(function()
{
<!--Start JQuery Ajax Requests-->
var pages;
var type;
$("input").click(function(event) {
    pages = $(this).attr("icr");
    type = $(this).attr("id");
    runRequest(0);
});

function runRequest(num){

    if (num > 3){
    $("#console").append("Finish!!!");
    return;
    }

    $.ajax
    ({
    type: "POST",
    url: "#",
    async: false,
    data: "type="+type+"&page="+num*1000,
    success: function(msg){
        $("#console").ajaxComplete(function(event, request, settings)
        {
            $("#console").append(msg);
            runRequest(num+1);
        });
    }
    });
}
});

这code都运行相同功能的3倍,乘以其发送的限制数量。但不知何故runRequest的num变量永远不会到达3,萤火虫控制台显示NUM为1或2,有时,它重复无限。如何避免它,让它只能运行3 syncronised电话?

this code have to run 3 times of the same function and send the limit number by multiplying it. But somehow runRequest's num variable never reaches the 3, firebug console shows num as 1 or sometimes 2 and it repeats infinite. How to avoid it and make it run only 3 syncronised calls?

推荐答案

我认为这个问题是您使用 .ajaxComplete的()。按照 jQuery的文档,它是一个单独的事件处理程序的注册方法。换句话说,你的成功函数登记事件来触发下完成时。我想,这也将导致你的第一个数据集被扔掉。

I believe the problem is your use of .ajaxComplete(). According to the jQuery docs, it is a separate event handler registration method. In other words, your success function is registering an event to fire upon the next completion. I think this would also cause your first data set to be thrown away.

这是额外的注意事项,您使用的是数据参数来传递数据。这被认为是一个对象,而不是一个查询字符串。如果使用GET Ajax调用,它就会被转换成一个查询字符串。 URL:TYPE =+类型+&放大器;网页=+ NUM * 1000,数据:{类型:类型,页面:NUM * 1000} 是等价的

An additional note, you're using the data parameter to pass in data. This is supposed to be an object, not a query string. If you use a GET Ajax call, it gets converted to a query string. url: "?type="+type+"&page="+num*1000, and data: { "type": type, "page": num*1000 } are equivalent.

尝试重写它是这样的:

$.ajax({
    type: "POST"
    url: "#",
    async: false,
    data: { "type": type, "page": num*1000 },
    success: function(msg) {
        $("#console").append(msg);
        runRequest(num+1);
    });
}

.ajaxComplete()方法多用于插入一个单独的函数调用或行为到每一个AJAX的成功处理程序(当你在相同的许多此类请求的目的页面,并希望他们每个人追加到控制台,除了做正常的回调)。像这样的:

The .ajaxComplete() method is more for the purpose of inserting a separate function call or behavior into every ajax success handler (when you have many such requests on the same page and want each of them to append to the console, in addition to doing their normal callback). Like this:

$(document).ready(function()
{
    $("#console").ajaxComplete(function(event, request, settings)
    {
        // $(this) is pointing to the #console ID because the function is bound to it
        $(this).append('Triggered ajaxComplete handler. The result is ' +
                 xhr.responseHTML);
        runRequest(num+1);
    });
    // variables

    // document event bindings/handlers

    // Ajax functions (you could move this outside of the .ready function

}

这篇关于jQuery的阿贾克斯循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆