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);
}
});
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.
Thanks in advance
There are two different types of issues that can cause admin-ajax. php slow server responses. The first is a backend CPU issue and the second is more of a frontend issue where you will notice third party plugins polling this file in your website speed tests.
Ajax requests do just that. With this, all codes following this one will execute immediately; you will not have to wait until the function returns. This way, your user would not have to wait for findItem() to return a result. Of course, because you still need an answer, you would have to include a callback.
Ajax's primary advantage is its ability to improve performance and usability for web applications. Ajax allows applications to render with no data. This reduces server traffic. Web developers can reduce the time taken to respond on both sides of the request.
response is the object always. In order to to get your data you have to use response. d. If you put only response in alert it will show you something like [Object] in the alert. Suppose, response contains a message "Ajax call made successfully" then to see the message you have to use response.
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:
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With