Solved

my javascript hangs the browser (make it unresponsive)

Posted on 2006-10-19
5
1,148 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

739 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