Execution order of javascript function

Posted on 2004-05-01
Last Modified: 2010-08-05

Ive got a function that calls another function which uses the Microsoft.XMLHTTP component to  fire a load of requests off to anther page. In the first function, i want to make a layer on my page visible, just telling the user not to navigate away from the page until the layer closes. My function is as so:

function CopySelected(theAction)
      var warningLayer = document.getElementById("divWarning"); = "visible";
      var controlIndex; // to hold the current ID within the loop of control objects
      var element; // to hold the value of the current control
      var numberOfControls = document.frmProjects.length; //counts all the form elements within the current form
      for (controlIndex = 0; controlIndex < numberOfControls; controlIndex++)
            element = document.frmProjects[controlIndex];
            //check whether the current control is a checkbox
            if (element.type == "checkbox")
                  if (element.checked == true)
                        //copy names to hidden fields
                        var theValue = element.value
                        var theRow = document.getElementById("row" + theValue);
                        if(theRow.className != "RedNormalText") {
                              if(theAction == "Close") {
                                    theRow.className = "GreyNormalText";
                                    } else {
                                          theRow.className = "BlackNormalText";
                        var IDList = document.getElementById("hdnIDList");
                        IDList.value = IDList.value + element.value + ',';
                  } //end if(element.checked == true)
            } //end if (element.type == "checkbox")
      } //end for loop
      // = "hidden";
      //no need to submit the form
      return false;

You can see in the above function that on the second line i make the layer visible, it is not for another 20 odd lines that the call to the other function is called "CloseProject(theValue,theAction);". However, the other function seems to execute before me layer is shown. Is there any way i can pause the above function until i am sure that the layer has been made visible??

Thanks in advance

Question by:higgsy
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10966800
A suggestion:
- on the onload of the new layer, set a variable X to true
- use a timeout-function to call the rest of your function
- this timeout-function should test the status of the variable X, if false then it recalls the timeout and exits, if true it continues

Accepted Solution

Phrogz earned 500 total points
ID: 10968353
Changes to the DOM for a web page are not actually reflected visually until the current event handler script is done.

If you want to make an item visible, and run other code AFTER it's been made visible (needed on some browsers to get a non-0 value for items like offsetWidth) then you need to use setTimeout to give the browsers a chance to redraw the page to reflect the new state.

For example:
function ShowItem(){
   setTimeout( ResizeItem, 10 );
function ResizeItem(){

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Diminish Pop-up  in 3 seconds 7 52
Google Analytics setup 5 51
multiple selects 23 49
JavaScript Loan Calculator Error 3 19
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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…
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…

821 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