简体   繁体   English

Ajax Call循环,Ajax jQuery

[英]Ajax Call loop, Ajax jQuery

I need and Advice here. 我需要和建议在这里。

I have this situation, trying to get some JSON Data, thought the AJAX (using jQuery). 我有这种情况,尝试获取一些JSON数据,想到AJAX(使用jQuery)。

The idea is it, I need more data then 1 or 2 Ajax Call to get all data what I need. 我的想法是,我需要更多数据然后1或2个Ajax Call来获取我需要的所有数据。

So, I need to make multi Ajax Calls and I try to find out the best way to do that (The best practice). 因此,我需要进行多个Ajax调用,并尝试找出最佳方法(最佳实践)。

One idea which came first in my head is something like this (it can have some syntax erros or even something worse, looking for a logic for now): 首先出现在我脑海中的一个想法是这样的(它可能有一些语法错误甚至更糟糕的东西,现在寻找逻辑):

var dataArr = {
    page:0,
    pageSize : 1000
};


var StoreData = [];

var getSomeData = function () {

    $.ajax({
        type : "GET",
        url : URL,
        data : dataArr,
        dataType : "json",
        error : OnLoadError,
        success :function(data, status){

            StoreData.push[data];
            if(data.length){
                dataArr.page = dataArr.page + 1
                getSomeData();
            }

        }
    });
};

Additional info: 附加信息:

It can be around 4-9 Ajax call to gett all data which is (4000-9000 items). 它可以在4-9个Ajax调用gett所有数据(4000-9000项)。

I tested pageSize : 1000 and works fine so I can get for sure 1000 items per call. 我测试了pageSize : 1000并且工作正常,所以每次通话我肯定可以获得1000个项目。

So if you can help me with some advice, some changes in existing one or another better way to do that, or even some example, I really appreciate 所以,如果你可以帮助我一些建议,现有的一些或另一个更好的方法来做到这一点,甚至一些例子,我真的很感激

Thanks Everyone ! 感谢大家 !

You could set a flag at the serverside when finished and put that into your status parameter for the success function. 完成后,您可以在服务器端设置一个标志,并将其放入成功函数的status参数中。 That would clean up your code. 这会清理你的代码。 Otherwise you could simply add a while loop, define your calls as synchronous and check if its finished within the loop condition. 否则你可以简单地添加一个while循环,将你的调用定义为同步,并检查它是否在循环条件内完成。

First of all, you need to optimize page size. 首先,您需要优化页面大小。 Defining it as 1000 is the easy way. 将其定义为1000是一种简单的方法。 If performance of the application is not important, don't read the rest of this answer :) 如果应用程序的性能不重要,请不要阅读本答复的其余部分:)

I suggest a trial-and-error based approach. 我建议采用基于试错法的方法。 Make a for loop, try different numbers for page-size (like 20, 50, 100, 500, 1000, 1500 etc). 制作一个for循环,为页面大小尝试不同的数字(如20,50,100,500,1000,1500等)。 Then log number of requests for each page size, and total time for all requests. 然后记录每个页面大小的请求数,以及所有请求的总时间。 Compare those numbers, and select your page size wisely. 比较这些数字,并明智地选择您的页面大小。

Secondly, if you are able to count the number of elements coming from server, then you may not have to send the unnecessary request at last. 其次,如果您能够计算来自服务器的元素数量,那么您可能不必最后发送不必要的请求。 Assume you are getting 4650 data. 假设您获得了4650个数据。 You get 1000, + 1000, +1000, +1000 and +650. 你得到1000,+ 1000,+ 1000,+ 1000和+650。 If you somehow figure out you are getting less than 1000, you wouldn't send the last request, would you? 如果你以某种方式弄清楚你的收入低于1000,你就不会发送最后一个请求,不是吗? Suppose the response format is something like: 假设响应格式如下:

<li class='item'>XXX</li>
// All list items 
<li class='item'>YYY</li>

Then count the number of li's with jquery, and if it is less than your page size, it means that you get all the data you need. 然后使用jquery计算li的数量,如果它小于页面大小,则表示您获得了所需的所有数据。

Lastly, if you know your total data has length more than 4000, you may want to send the first 4 requests asynchronously. 最后,如果您知道您的总数据长度超过4000,您可能希望异步发送前4个请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM