What I'm trying to do seems so simple but I can't figure it out.
Here's the basic scenario:
Function A is running
Function B starts when a button is clicked
I need to disable Function A while Function B is running
Then when Function B is complete, re enable Function A (back to where we started).
I have setup a very basic Pen to help illustrate what I'm trying to achieve. You can ignore the fact that the functions add classes, this was just to have it do something.
https://codepen.io/andystent/pen/yLNbyjZ?editors=1111
So in the example the desired scenario would be:
Block is green
Block changes to red when button is clicked
After changing to red, block changes back to green
Again, ignore the color changes and adding/removing classes - that is just to have something happen.
JS from the Pen example:
var block = document.getElementById("block");
var button = document.getElementById("button");
//Function A: the main function that is active by default - I need this function disabled/stopped when the button is clicked, and then re enabled when Function B is complete.
$(document).ready(function(){
block.classList.add("green"); //adding class is only for example
});
//Function B
$(button).click(function(){
block.classList.add("red"); //adding class is only for example
});
UPDATE 1
This is my working based on answers from Jasper and Adder.
Using the separate functions works great but when I click the button the console.log works but I can still do what's happening in the if statement, as if active = true.
In this example, when I click the button it should change active to "false" and therefore not allow the if statement's content to work. I hope that makes sense....
var aActive = true;
// FUNCTION A - Active is True
function fA(){
aActive = true;
console.log("active = true");
};
//FUNCTION B - Active is False
function fB(){
aActive = false;
console.log("active = false");
}
//When clicking Nav link run Function B (active false)
var navLinks2 = document.querySelectorAll("#nav-wrap nav a")
navLinks2.forEach(function (navLink2) {
navLink2.addEventListener('click', function () {
fB();
//fA();
});
});
//Only do this if Active is TRUE
if(aActive) {
//do something
} //end if active