[thelist] Javascript: run a function once a function is complete

sam foster potatosculptor at gmail.com
Mon Jan 9 16:23:19 CST 2006


The way I've dealt with this is to use the EventDispatcher class / pattern
(I believe Flash ships with an implementation, the one I was using was
ported over to (browser-based) javascript by a colleague). You can register
objects as being capable of dispatching and receiving events. Then your
onComplete handler would dispatch an event, and all registered listeners
would receive it and handle it as appropriate. I think thats the kind of
thing you are looking for - the completion of your showResponse (onComplete
handler) is really an event, and you need a means of broadcasting that event
so other things can happen.

Sorry I dont think I can just send you the source, anyone know of a similar
implementation that is freely distributable? This might get you thinking and
searching in the right direction anyhow.

hth
Sam

On 1/9/06, ben morrison <morrison.ben at gmail.com> wrote:
>
> > Depends, if A is an AJAX load thingy then you need to monitor the
> > readyState to see when the whole thing was loaded (4), and also
> > provide a timeout fallback in case it simply takes too long.
> >
> >
> Yes im currently using the protoype ajax call so im using
>
> onComplete: showResponse
>
> which is my function:
>
> function showResponse(originalRequest) {
>             //put returned XML into xmlDoc for processing...
>              var xmlDoc = originalRequest.responseXML;
>              xmlDoc = xmlDoc.documentElement;
>
>               //Regular expression used to match any non-whitespace
> character
>             var notWhitespace = /\S/;
>             for (var i=0; i<xmlDoc.childNodes.length; i++) {
>                   if ((xmlDoc.childNodes[i].nodeType ==
> 3)&&(!notWhitespace.test(xmlDoc.childNodes[i].nodeValue))){
>                       xmlDoc.removeChild(xmlDoc.childNodes[i])
>                       }
>                 }
>              for (var i=0; i<xmlDoc.childNodes.length; i++) {
>                 objPrefs.prefs[i] = xmlDoc.childNodes[i].nodeName;
>                   objPrefs.prefValue[i] = xmlDoc.childNodes
> [i].firstChild.nodeValue;
>              }
>              alert("Loaded");
>              objPrefs.loaded = true;
>          }
>     },
>
> My other function needs to make sure this has worked before i call it
>
>     //return the value of the preference
>     get: function(prefname) {
>         if (this.loaded){
>             var i = this.prefpos(prefname);
>             if (i >=0) {
>                 return this.prefValue[i];
>             } else {
>                 return "BadFieldName!";
>             }
>         } else {
> //try and make sure its been transformed
>             setTimeout("objPrefs.get('"+prefname+"')", 1000);
>             }
>
>     },
>
>
> As you can see there is too much recursion going on there, any ideas?
>
> ben
> --
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>



More information about the thelist mailing list