I'm with Jonathan on this: The blocking (which in many browsers will completely lock up the UI, making the browser completely nonresponsive) makes for a bad user experience, and you can almost always do it better using callbacks. Takes a bit more thought, but it's worth it. -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available
On Jan 23, 3:43 pm, "Jonathan Rosenberg" <[email protected]> wrote: > While this will "fix" the problem, it is bad practice to cause blocking in > Javascript. Particularly so in this case, since the > poster stated that the request takes a long time to fulfill. Remember that > Javascript is single-threaded. > > I'm relatively new to serious Javascript programming, but every time I've > made an Ajax request synchronous, I've ended up having to > change it. > > My advice: spend the time to wrap your head around the Javascript execution > model. You'll be grateful later. > > -- > Jonathan Rosenberg > Founder & Executive Director, Tabby's Placehttp://www.tabbysplace.org/ > > > -----Original Message----- > > From: [email protected] > > [mailto:[email protected]] On > > Behalf Of eulerss > > Sent: Friday, January 23, 2009 10:22 > > To: Prototype & script.aculo.us > > Subject: [Proto-Scripty] Re: Question from an almost ajax newbbie > > > my problem was very similar, i only added the next line into my > > script: > > > asynchronous: false, > > > and it works fine > > > eulerss > > > On 23 ene, 08:12, "[email protected]" <[email protected]> > > wrote: > > > I get an hint that we are 'compaesani' :-P > > > > Anyhow.. thank you very much,I'm studying that link you gave me, > > > meanwhile, in my particular case setting to "syncronous" has indeed > > > resolved my issue. > > > > Thank you very much for you toughtfull response > > > > A presto > > > > David(e) > > > > On Jan 23, 2:08 pm, "T.J. Crowder" <[email protected]> wrote: > > > > > 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-re... > > > > 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, "[email protected]" <[email protected]> > > > > 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- Ocultar texto de la cita - > > > > - Mostrar texto de la cita - > > --~--~---------~--~----~------------~-------~--~----~ 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---
