zq29 Posted November 9, 2006 Share Posted November 9, 2006 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 :) Quote Link to comment Share on other sites More sharing options...
tomfmason Posted November 9, 2006 Share Posted November 9, 2006 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 Quote Link to comment Share on other sites More sharing options...
zq29 Posted November 9, 2006 Author Share Posted November 9, 2006 Thanks for the suggestion Tom, I'll give that a go shortly! Quote Link to comment Share on other sites More sharing options...
zq29 Posted November 16, 2006 Author Share Posted November 16, 2006 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? Quote Link to comment Share on other sites More sharing options...
fenway Posted November 16, 2006 Share Posted November 16, 2006 Simply keep updating a success variable with the various states of each function -- sort of like a bit flag. Quote Link to comment Share on other sites More sharing options...
zq29 Posted November 16, 2006 Author Share Posted November 16, 2006 [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. Quote Link to comment Share on other sites More sharing options...
fenway Posted November 16, 2006 Share Posted November 16, 2006 A less elegant way would just be to keep a counter -- personally, I like bit flags much better, because you can have lots of functions and nothing is too cryptic. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.