Link to home
Start Free TrialLog in
Avatar of manish_regmi
manish_regmi

asked on

my javascript hangs the browser (make it unresponsive)

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
ASKER CERTIFIED SOLUTION
Avatar of VovinE
VovinE

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of manish_regmi
manish_regmi

ASKER

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
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.
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.

thanks VovinE for your help.

regards
Manish Regmi