ldsmike88 Posted March 17, 2007 Share Posted March 17, 2007 I am trying to have a global variable be set to 'no' when a certain function is not being run and set to 'yes' when it is being run. I can't seem to get it to work though. I'm not sure I am distinguishing between local and global correctly. Here is what I have: var working = 'no'; function myFunction(variableOne, variableTwo) { if(working == 'no'){ working = 'yes'; //time delayed function working = 'no'; } else { alert('Processing another request. Please wait.'); } } Quote Link to comment Share on other sites More sharing options...
emehrkay Posted March 17, 2007 Share Posted March 17, 2007 you set it to yes then to no again... Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 17, 2007 Author Share Posted March 17, 2007 I try to set it to yes, but before I set it to no again it goes through a bunch of code that isn't executed instantly. It takes up to ten seconds sometimes to run the code between yes and no. My goal is when someone runs the function and tries to run the same function somewhere else before the first one is done running it will give them the alert message. Quote Link to comment Share on other sites More sharing options...
fenway Posted March 20, 2007 Share Posted March 20, 2007 You don't have any local variables there. Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 20, 2007 Author Share Posted March 20, 2007 I don't think I want local variables... do I? If the function is already being run I don't want to be able to try to start running it again until it is done. Why aren't the global variables working? Thanks! Michael Quote Link to comment Share on other sites More sharing options...
fenway Posted March 20, 2007 Share Posted March 20, 2007 Should be working, usually one sees a boolean flag being used for something like this. Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 21, 2007 Author Share Posted March 21, 2007 What's a boolean flag? Quote Link to comment Share on other sites More sharing options...
fenway Posted March 21, 2007 Share Posted March 21, 2007 Like you've done, but with true/false. Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 23, 2007 Author Share Posted March 23, 2007 Well the true/false worked for that part, but now I need to be able to set a global variable. It isn't going to be a 'one' or 'the other' kind of deal, it will have several different possible values. How do I make that work? Thanks! Michael Quote Link to comment Share on other sites More sharing options...
fenway Posted March 23, 2007 Share Posted March 23, 2007 I'm not sure what you mean... from the sounds of it, you just wanted to make sure the function didn't execute a second time before it was finished. Quote Link to comment Share on other sites More sharing options...
ldsmike88 Posted March 23, 2007 Author Share Posted March 23, 2007 Correct. Now that I have done that I want to add another statement in the if statement. For example: Function doThis(myAction); if(working == false || action == myAction) working = true; location = myLocation; //Run time delayed action. } else { alert('already working'); } This way if someone clicks on a certain button it will run the script. If they click on another button before it finishes it will alert them. If they click on the original button while it is working it will restart the function and run the script. This way if it gets stuck in the middle of the function and stops running it will start over again and and you wont have to refresh the page because the alert keeps poping up. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.