i usually use the first method you described; a global var maintained by 2 functions, start and stop. the other way is too complicated imo :)
AdamV wrote: > I'm writing an AJAXy application and when I start doing an ajax call, > I want to block the UI and show a loading graphic until the loading is > done. For various reasons, a number of different functions might > initiate a load and a number might signal its end, and in a particular > sequence two such functions might be called during one "ajax load" > event. > > As I was thinking about opimizing JavaScript anyways, I figured I see > if anyone has any thoughts about optimizing the general situation > where you have a "start" function that might be called once its > started and a "stop" function that might be called when nothing is > currently running. > > The trivial way to write this would be: > > var running=false; > > function do_start() { > if (running) return; > running = true; > > // Do stuff; > } > > function do_stop() { > if(!running) return; > running = false; > > // Do stuff; > } > > However, the following would also work: > > function nullFunct() {}; > function startFunct() { > // Do Stuf; > do_stop = stopFunct; > do_start = nullFunct; > } > function stopFunct() { > // Do Stuf; > do_stop = nullFunct; > do_start = startFunct; > } > > var do_start = startFunct, do_stop=nullFunct; > > > Does anyone have any thoughts about taking either approach? I would > suspect that the later approach gets more useful as the test for > "should we allow stuff to happen here" gets ore complicated than I've > shown. > > Adam > >