I often want to trigger a certain function just once, but I need to trigger it from within another function that gets repeatedly called. For instance, taking a snapshot of something for later use. I usually do it by setting a global boolean.
I'm wondering whether the way I do it is actually best way?
I seem to recall reading that global variables are bad, and global boolean variables are even worse!
Anyway, this is how I usually accomplish triggering a certain method just once:
In my initial set of variables...
private var myStatus:Boolean = false;
Then, within the function that gets called often...
if (!myStatus) {
doMyFunction();
myStatus = true;
}
It seems fairly logical to me, but is it the right thing?
UPDATE: Well, based on what I learned from your answers, instead of checking a global boolean variable, I now first check whether the XML node exists (I am storing the images within an XML structure before any writing to disk occurs), and, if it doesn't, then I append a new node with the base64 encoded image data. I do still set a boolean flag so that later on I can overwrite the blank image with user edited image data if need be. It works perfectly. Thank you all for your help!
I also now feel more comfortable about using that particular (thread unsafe) system in certain situations.