Jump to content

Several onclick events, sometimes not all of them get executed


zq29

Recommended Posts

I have a button that runs three javascript function calls when clicked. Sometimes it doesn't execute all of the functions though... It allways runs the first (showHide()), although it doesn't always run one of the two calls to requestForm(). Any ideas what could be causing this?

[code]<input type='button' name='add' value='Add' onclick='showHide(1,"dialogue_sec","addHotelRoom","0"); requestForm("displayHotels",0,"hotel_container"); requestForm("displayRooms",0,"room_container");'/>[/code]

As for the three function calls I'm running:
- showHide() - shows/hides a div
- requestForm() - An AJAX function, requests some data from a database, in the two cases above it builds two select boxes.

Here are my two functions code, if it makes any difference:
[code]function showHide(show,targetDiv,cmd,uID) {
    switch(show) {
        case 1:
            document.getElementById(targetDiv).style.display = "block";
            requestForm(cmd,uID,targetDiv);
            break;
        case 0:
            document.getElementById(targetDiv).style.display = "none";
            break;
    }
}

function requestForm(cmd,uID,targetDiv) {
    var oXmlHttp = zXmlHttp.createRequest();
    oXmlHttp.open("get","fetch.php?cmd="+cmd+"&id="+uID,true);
    oXmlHttp.onreadystatechange = function() {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {
                document.getElementById(targetDiv).innerHTML = oXmlHttp.responseText;
            } else {
                document.getElementById(targetDiv).innerHTML = "An error occured: "+oXmlHttp.statusText;
            }
        }
    };
    oXmlHttp.send(null);
}[/code]

Is there a way of running the second function after the first has been fully executed, and likewise, running the third when the second has finished?

Many thanks in advance guys :)
Link to comment
Share on other sites

Yea what I would do in this case is have another function that would call the other three and in the order that you need them..

Now as far as preforming the functions once the first pervious has completed...

Maybe this example will help..

[code=php:0]
function something() {
  var function1 = yourfunction();
  if (function1 == true) {
      var function2 == yourSecondfunction();
      if (function2 == true) {
          thirdFunction();
      }
  }
}[/code]

Now as your event trigger all you should have to do is run the something() function..

Good Luck,
Tom
Link to comment
Share on other sites

Ok, so I have given that theory a go, and it doesn't work the way I was hoping. I think that it [i]would[/i] work if the functions were fully executed almost instantly, although, on occasion it could take a few seconds for any one function to execute due to the fact that some of them are AJAX based and are pulling (sometimes) large amounts of data from MySQL, there will also be the possibility of around 20-30 users trying to pull this same information at the same time (although on a local intranet).

I can't think of the method at the moment, but does anyone think that this could be possible using a 'do while' to check that a previous function has fully executed before it executes the next one?
Link to comment
Share on other sites

[quote author=fenway link=topic=114390.msg469144#msg469144 date=1163707397]
Simply keep updating a success variable with the various states of each function -- sort of like a bit flag.
[/quote]Interesting, I'll give that a go - Thanks.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.