Hi, The problem with your `nick` function is that it expects to be able to return a value it's gotten from the server, but the call to the server is *asynchronous* -- your function returns before the call completes, and so your function always returns "".
You _could_ make the ajax call synchronous instead, but that's a very bad idea at the moment. Synchronous ajax calls lock up the UI of the browser while the call is running (either freezing just your page, or on some browsers completely freezing the entire UI -- even tabs containing unrelated pages). This makes for a bad user experience and so synchronous calls need to be avoided. So instead, we want to change how `nick` works so that it doesn't _return_ a value but still does what you want it to do. The way to do that is to have it call a callback when it gets the value. In this particular case, you're making several calls to `nick`, which complicates things. I would change chat.php so that it accepts a *list* of "nano"s to look up and returns the list of nicks. That way you only make one call to the server instead of a call for each "nano". That not only simplifies things, but makes it faster as well, since each call to the server is expensive. The result would look something like this (also here: http://pastie.org/1099565): * * * * function nickList(nanos, callback){ // Send chat.php a list of nanos delimited by | var parametros="nanos="+encodeURIComponent(nanos.join("|")); new Ajax.Request('chat.php',{ method: 'GET', parameters: parametros, onSuccess: function(resp){ // Call the callback with the response, split on | callback(resp.responseText.split("|")); } }); } function cpvm(){ var antiguo=$("privadosant").innerHTML; var parametros="cpvm=si"; new Ajax.Request('chat.php',{ method: 'GET', parameters: parametros, onSuccess: function(resp){ var i, salasdiv, entries, count, nanos; // Cache some things so we aren't doing unnecessary work salasdiv=$("salas"); entries=resp.responseText.split("|"); count=entries[0]; antiguo.innerHTML=count; nanos = []; for (i=1; i<=count; ++i) { if(salasdiv.innerHTML.match(entries[i])==null) { nanos.push(entries[i]); } } if (nanos.length > 0) { nickList(nanos, nickResponse); } function nickResponse(nicks) { var i, newdicv, divid; for (i=1; i<=nicks.length; ++i) { newdiv = document.createElement('div'); divid = nicks[i]; newdiv.setAttribute('id',divid); newdiv.innerHTML = nicks[i]; salasdiv.appendChild(newdiv); } } } }); } * * * * I've changed `nick` to `nickList` and changed the parameter name from `nickk` to `nanos`. `nickList` accepts an array of "nanos" to look up and a callback function to call; it sends the list to chat.php as a |- delimited string, and when that comes back it calls the callback with an array (it expects the response to be |-delimited and IN THE SAME ORDER as the "nanos" array was). `cpvm` has been changed to first build up a list of all of the "nanos" it needs to look up and then, if there are any, it calls `nickList` to do it, giving `nickList` a callback to call. The callback processes the result by adding the divs to `salas`. The above is completely untested, but should get you going the right direction. With apologies, several off-topic comments: 1. You were repeatedly looking up the `salas` div (by having `salasdiv= $("salas");` inside your loop). Looking up elements can be expensive, you don't want to keep doing it unnecessarily; just do it once and then reuse it. 2. You were constantly re-splitting the response text in `cpvm`. Splitting a string isn't all that much work, but still, why constantly re-do it? What if you change the delimiter for some reason? You'd have to make sure you changed all of the calls to `split`. Much better to do it once at the outset. 3. I replaced your `while` loop with a `for` loop. It all amounts to the same thing, but if you're doing a loop based on a counter, it's best to use a `for` loop. 4. You didn't declare the `i` variable, which means you were creating and using a global, which is probably not what you wanted. Buena suerte! Espero que esto ayude, -- T.J. Crowder Independent Software Consultant tj / crowder software / com www.crowdersoftware.com On Aug 17, 4:48 pm, Canino_latino <[email protected]> wrote: > Hi, I'm trying to return value from onSuccess I saw somes posts but it > doesn't help me. > > this is my code: > > function nick(nano){ > var respuesta = ""; > var parametros="nickk="+encodeURIComponent(nano); > new Ajax.Request('chat.php',{ > method: 'GET', > parameters: parametros, > onSuccess: function(resp){ > respuesta = resp.responseText; > } > }); > return respuesta; > > } > > and I call the function here: > > function cpvm(){ > var antiguo=$("privadosant").innerHTML; > var parametros="cpvm=si"; > new Ajax.Request('chat.php',{ > method: 'GET', > parameters: parametros, > onSuccess: function(resp){ > antiguo.innerHTML=resp.responseText.split("|")[0]; > i=1; > while (i<=resp.responseText.split("|")[0]) > { > var salasdiv=$("salas"); > > if(salasdiv.innerHTML.match(resp.responseText.split("|")[i])==null) > { > var newdiv = document.createElement('div'); > var divid = resp.responseText.split("|")[i]; > newdiv.setAttribute('id',divid); > newdiv.innerHTML = > nick(resp.responseText.split("|")[i]); > salasdiv.appendChild(newdiv); > } > i++; > } > } > }); > > > > } -- 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.
