Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pause code while waiting for response 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
I have a handrolled AJAX js file I use, the problem is if I then use the makeRequest() function from within a function and have code below this call, the code continues to run before a response it returned from the AJAX call.

I have gotten round this by having the makeRequest() function call another function with return vars, which i then query to work out which function called the AJAX and continue processing.

Although this works, it's a bit ugly, so how do I make the code below the makeRequest() call wait till the response is returned or I guess another way would be is it possible to call a routine that is held in a variable.

eg. I call makeRequest(url,vars,functionname), passing the name of the function I want to be called, then in the AJAX funtion when finished i want it to call the function in the variable functionname. is this possible?

thanks 1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
If the third parameter to the open method is false, the ajax call will not run asynchronously and will wait until the call has returned before continuing to the next instruction. To use this you'll probably have to revise your makeRequest() function, or write a new synchronous version.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
not really what I wanted to do, Is my second idea possible with the function call to a function held in a var ?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Yes, your second way is entirely possible. Simply split off the code you want executed after the ajax call into a separate function, and pass the NAME of that function (without the parens after it) to your makeRequest function. When the ajax call completes successfully, call the function passed to you (just use the parameter name as the function name and add parens after it. For example:
Code:
function makeRequest(url,param,callback) {
   ...
   callback();
}
function callBackFunc() {
   alert("here i am");
}
...
makeRequest(daURL,somevar,callBackFunc);

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
wow- just what i needed, so you simply use variable_name(); and it will actually substitute for the content of the var_name and call that function.

Spot on Tracey [cheers]

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
i've implemented your suggestion and it works great - thanks very much, appreciated!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
You're quite welcome. That's what we're here for.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top