Ciao David, By default, Ajax requests are asynchronous, and so they get *started* when you do "new Ajax.Requst" but then they run for a while and complete later; meanwhile, your code that started the request continues. So what your anonymous function above returns will be whatever value risultato has when the function starts, since the request hasn't completed yet. Later, at a time you cannot predict, the value of risultato will be updated by the callbacks you've registered.
You _can_ make requests synchronous if you like (set the asynchronous parameter to false), but that will lock up the UI of the browser for the entire time the request is taking pace -- which from what you've said about availability.php non sembra una buona idea. This might be useful: http://proto-scripty.wikidot.com/prototype:how-to-bulletproof-ajax-requests It's an example of an Ajax request that starts the process of retriving a record from the server, and then displays that record (or, of course, an error) on callback. Basically, when working with lots of asynchronous stuff like this, you end up modifying your logic so you don't make a function call and expect an answer back, but instead you make a function call and expect to handle the result in callbacks. HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On Jan 23, 12:38 pm, "david.0pl...@gmail.com" <david.0pl...@gmail.com> wrote: > First of all I'm David, from Italy. > I recently started looking into your fabolous prototype thanks to this > script:http://tetlaw.id.au/view/javascript/really-easy-field-validation > > After some playing around I decided to add another validation check. > Without going into the details of the other script (which is not > relevent) I would like to ask you how can I return a variable AFTER an > Ajax request has finished. > > This is the code: > > function(v) { > new Ajax.Request('availability.php', > {method: 'get', parameters: {validate: v}, > onSuccess: function( transport ) { > if( transport.responseText.match(/true/)) { > risultato = false; > } > else { > risultato = true; > } > } > }); > return risultato; > } > > The problem is that availability takes a long time to process the > response (1sec) and while he process the thing risultato has the value > of the last query. > > For example: first time we have risultato = true; > so it returns: true > The second time I invoke the function i still have risultato = true > from before. Now the ajx request returns risultato = false BUT while > it goes the functions returns true. > > How can I avoid this? > > Thank you very much --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---