Solved

my javascript hangs the browser (make it unresponsive)

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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 discusses how to create an extensible mechanism for linked drop downs.
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now