Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX call freezes browser for a bit while it gets response and executes success

I am doing an AJAX call to my webserver which fetches a lot of data. i show a loading image that spins while the ajax call is executed and then fades away.

the thing i have noticed is that all of the browsers on this particular call will make it non-responsive for about 7 seconds. That being said, the loading image is NOT spinning as what i had planned while the fetch was occurring.

I did not know if this was something that happens or if there is a way around to, in a sense cause there to be a fork() so that it does 1 thing, while my loading icon still spins.

THoughts? Ideas?

below is the code as someone wanted to see it:

$("div.loadingImage").fadeIn(500);//.show();
            setTimeout(function(){
            $.ajax({
                type: "POST",
                url: WEBSERVICE_URL + "/getChildrenFromTelTree",
                dataType: "json",
                async: true,
                contentType: "application/json",
                data: JSON.stringify({
                    "pText": parentText,
                    "pValue": parentValue,
                    "pr_id": LOGGED_IN_PR_ID,
                    "query_input": $("#queryInput").val()
                }),
                success: function (result, textStatus, jqXHR) {
                    //alert("winning");
                    //var childNodes = eval(result["getChildrenFromTelTreeResult"]);
                    if (result.getChildrenFromTelTreeResult == "") {
                        alert("No Children");
                    } else {
                        var childNodes = JSON.parse(result.getChildrenFromTelTreeResult);
                        var newChild;
                        //alert('pText: '+parentText+"\npValue: "+parentValue+"\nPorofileID: "+ LOGGED_IN_PR_ID+"\n\nFilter Input; "+$("#queryInput").val() );
                        //alert(childNodes.length);
                        for (var i = 0; i < childNodes.length; i++) {
                            TV.trackChanges();
                            newChild = new Telerik.Web.UI.RadTreeNode();
                            newChild.set_text(childNodes[i].pText);
                            newChild.set_value(childNodes[i].pValue);
                            //confirmed that newChild is set to ServerSide through debug and get_expandMode();
                            parentNode.get_nodes().add(newChild);
                            TV.commitChanges();
                            var parts = childNodes[i].pValue.split(",");
                            if (parts[0] != "{fe_id}" && parts[0] != "{un_fe_id}") {
                                newChild.set_expandMode(Telerik.Web.UI.TreeNodeExpandMode.ServerSide);
                            }
                        }
                    }
                    //TV.expand();
                    //recurseStart(TV);
                },
                error: function (xhr, status, message) {
                    alert("errrrrror");
                }
            }).always(function () {
                    $("div.loadingImage").fadeOut();
                });
                },500);

A corworker of mine noticed this issue, and suggested i add a setTimeout(function(){..},500); but it does not fix the issue at hand, so it will most likely be removed.

like image 217
Fallenreaper Avatar asked Aug 15 '12 17:08

Fallenreaper


People also ask

Why is AJAX success not working?

ajax post method. The reason was my response was not in the JSON format so there was no need for the dataType: 'json' line in the submit method. In my case, the returned response was in text format that's why it was not going to success event. Solution: Remove dataType: 'json' line.

What happens during an AJAX call?

When you make an AJAX request, your browser sends an HTTP request to a given address. The server on the other end of the request responds, and returns the data to your browser. This is the same thing that happens when you navigate to a new web page.

What is success response AJAX?

Response is the object passed as the first argument of all Ajax requests callbacks. This is a wrapper around the native xmlHttpRequest object. It normalizes cross-browser issues while adding support for JSON via the responseJSON and headerJSON properties.

Is there a limit to AJAX calls?

The AJAX Toolkit uses SOAP API calls. SOAP calls always have a maximum limit of 50MB. However, it is also XML-based, which restricts the available characters you can use, so the file has to be Base-64 encoded. This puts the final limit at around 37 MB of binary data, as you've observed.


1 Answers

Since JavaScript is single threaded, a lot of sync processing will hang up the event queue and prevent other code from executing. In your case, it's the for-loop thats locking up the browser while it's executing.

What you can try is putting all your iterations into your event queue.

for (var i = 0 ; i < childNodes.length ; i = i + 1) {
    (function(i) {
        setTimeout(function(i) {
            // code-here
        }, 0)
    })(i)
}

This should space out the processing and not force the browser to finish them all at once. The self executing function is there to create a closure to hold on to the value of the loop counter i.

like image 153
biswarup Avatar answered Oct 20 '22 01:10

biswarup