tags:

views:

32

answers:

2

Is there any way I can run this code with async:false? I notice the jQuery manual specifies that it must be true, when passing the call return to a variable, but that is painful considering the method is meant to be generic.

ajaxCall.classFunction = function( $class, $function, $params){

//Ensure no errors occured
$params = typeof($params) != 'undefined' ? $params : false;

//Ajax Call for data
var $r =$.ajax({
    type:   'POST',
    url:    'json.php?c='+$class+'&func='+$function,
    data:   $params,
    timeout:5000,
    async:false,
}).responseText;
return $r;
A: 

What would the method being generic have to do with its being asynchronous?

Liam Bailey
This should have been a comment. It is not an answer to the question. You are asking for clarification. But i support your question.
elusive
What I understand from the async: false is that the page is stuck loading whilst the url is called. If I plan to use this in anything that can work in the background, that makes this difficult.
Alex
That's true. Javascript execution will be blocked until the ajax call finished.
Jakub Konecki
+1  A: 

No, you will have to specify success callback. With async set to true your outer method is not blocked and returns immediately without waiting for the actual response for your ajax call.

Jakub Konecki
did you mean: With async set to *true* your outer method is not blocked and returns immediately.
dave thieben
Yes, updated the answer. Cheers!
Jakub Konecki
So what your saying is that it should be possible to add a success handler which can then assign the output to a variable( or etc )?
Alex
Yes. Inside callback you can reference a variable that is declared outside it (you might want to read about closures - http://en.wikipedia.org/wiki/Closure_(computer_science)). But your ajax call has to by synchronous.
Jakub Konecki