当响应非常大时,JQuery ajax会冻结ui [英] JQuery ajax freezes ui when response is very large

查看:172
本文介绍了当响应非常大时,JQuery ajax会冻结ui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用JQuery ajax函数并且响应非常大~1mb时,ui会在调用success函数之前被冻结。我已经通过JSON.parse函数和解析大量数据来体验这一点。我相信这个函数用于返回将内容格式化为JSON的请求。这是我正在使用的代码。

When I use JQuery ajax function and the response is quite large ~1mb the ui gets frozen just before the success function is called. I have experienced this with the JSON.parse function and parsing a large amount of data. I believe that this function is used on the return of the request to format the content into JSON. Here is the code I am using.

$.ajax({
        url: "/sessions/" + this.get("session_id") + "/get_sample_data",
        data: params,
        dataType: 'json',
        type: "GET",
        success: function (response) {
            success(response);
        }
    });

无论如何都要覆盖响应的代码,这样我就可以将解析分成几部分并希望最小化对ui的阻塞?还是有另一种方法来解决这个问题。我正在使用铬和铬金丝雀,我在两者中得到相同的结果。

Is there anyway to override the code for the response so I can stagger the parsing into parts and hopefully minimise the blocking on the ui? or is there another way to fix this. I am using chrome and chrome canary and I get the same result in both.

提前致谢

推荐答案

猜测是解析一个巨大的JSON响应是导致延迟的原因。如果是这种情况,那么您有以下选择:

The guess is that parsing a giant JSON response is what it causing the delay. If that's the case, then you have these options:


  1. 将服务器响应分解为多个请求。

  2. 将解析分解为多个部分。

  3. 将响应发送给Web worker并在Web worker中解析(在旧版浏览器中不起作用)。

要将解析分解为多个部分,您必须更改jQuery ajax调用以返回原始文本,您必须创建自己的JSON解析器,可以在 setTimeout()上以块的形式工作,这样UI可以在解析时保持活动状态。这将是一项可以接受的工作量。我假设您从一个现有的JSON解析器开始,然后您必须修改它以便以一种可以在块中工作的方式保存它的状态。

To break up the parsing into multiple pieces, you would have to change the jQuery ajax call to just return the raw text and you'd have to create your own JSON parser that could do the work in chunks on setTimeout() so that the UI could stay alive while parsing. This would be a reaonable amount of work. I assume you'd start with an existing JSON parser and then you'd have to modify it to save it's state in a way that it could work in chunks.

更改如果您可以适当地修改服务器的接口,那么检索JSON片段的服务器接口可能是解决问题的更简单方法。

Changing the interface to the server to retrieve pieces of JSON is probably the easier way to solve the problem if you can modify the interface to the server appropriately.

对于某些替代想法如何以块的形式处理大数据,你可以看到在不阻止UI的情况下迭代数组的最佳方法

For some alternative ideas on how to process large data in chunks, you can see Best way to iterate over an array without blocking the UI

这篇关于当响应非常大时,JQuery ajax会冻结ui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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