Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

my javascript hangs the browser (make it unresponsive)

Posted on 2006-10-19
5
Medium Priority
?
1,154 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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

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…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

670 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