Jump to content

Global Variables


ldsmike88

Recommended Posts

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.');
}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.