Solved

my javascript hangs the browser (make it unresponsive)

Posted on 2006-10-19
5
1,150 Views
Last Modified: 2007-12-19
Hello All,
    The java script i am working on makes the browser unresponsive. The browser becomes responsive after the javascript function finishes.

The script is huge and is used to communicate with server using SOAP messages (called through 3rd party API).

How can i resolve this?

Thanks in Advance.
Manish
0
Comment
Question by:manish_regmi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
VovinE earned 500 total points
ID: 17763829
I this may be because of the loop in javascript.
If there is a loop waiting for response to arrive, then it may hang your browser.

You should get rid of the loop and set a callback function (onreadystatechange) for your XMLHttpRequest object, to handle data coming back asynchronously.
0
 
LVL 8

Author Comment

by:manish_regmi
ID: 17764098
ok. i see. thanks.

so. i need to change my code to make it asynchronous.

But i have one such request in loop how can i make it asynchronous.

eg

function tmp()
{
    do
            {
                  tmp = getParentName(idarr[idx-1]);
                  if(tmp === false)
                        break;

                  idarr[idx] = tmp;
                  idx++;
            //      alert(idarr);
            }while(1);
}

getParentName does some SOAP request. I can add an extra parameter for a callback routine but how to implement loop logic.

Thanks
0
 
LVL 6

Expert Comment

by:VovinE
ID: 17764184
well, you would need to make getParentName function asynchronous.
It seems, it waits until the soap request is finished - and inside this function loop causes browser to hang.

SOAP requests should be simple enough not to make several calls to request for data.
You might try to refactor your code, so only one SOAP call is required - which would return an array you need, not to make call for each item.
0
 
LVL 6

Expert Comment

by:VovinE
ID: 17764234
If you still need to implement loop asynchronously without refactoring the webservice, then:

implement a function, that starts precalculation (in pseudocode):

var idarr= new Array();
var idx;
function PerformCalculation()
{
  // todo: initialize your array
   StartRequest(myCallback); //you need to make sure everything is set up (SOAP Header and so on)
  // it should set asynchronous callback on result to myCallback
}
function myCallback(result)
{
         if(result == false){
            CalculationFinished(idarr);
        } else {
           idarr[idx] = tmp;
           idx++;
           StartRequest(myCallback);
        }
}

function CalculationFinished(arr)
{
   // do whatever you need with the result :)
}

StartCallback should return immedietaly after setting asynchronous callback.

0
 
LVL 8

Author Comment

by:manish_regmi
ID: 17764274
thanks VovinE for your help.

regards
Manish Regmi
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question