I want to make a function stop working when a button is clicked in Javascript.
I try to use the if statement to check if the button is clicked or not. Also, I have tried to make the function stop working when a certain condition met inside another function.
However, it still doesn't work anymore.
Does anyone know how to make it works and why the onclick == true doesn't work anymore?
let textarea = document.getElementsByTagName('textarea')[0]
let message = document.getElementById('message')
function clicks() {
console.log('hi')
//I want to make the appear function stop working when this function is executing.
}
function appear() {
//this is not working
if (document.getElementsByTagName('button')[0].onclick == true) {
message.style.display = 'none'
console.log('it works')
} else {
message.style.display = 'revert'
textarea.value = textarea.value.trimStart();
}
};
<textarea></textarea>
<div id="message">.....</div>
<button onclick="clicks()">split(20px)</button>
onclick is an event, which fired, when click event occurred. As a result, onclick doesn`t store state. You need to create variable, visible for clicks and appear functions, and use it to identify the flag to stop execution of function appear.
let textarea = document.getElementsByTagName('textarea')[0];
let message = document.getElementById("message");
let stopButtonClicked = false;
function clicks() {
console.log("hi");
stopButtonClicked = true;
}
function appear() {
if (stopButtonClicked) {
message.style.display = "none";
console.log("it works");
} else {
message.style.display = "revert";
textarea.value = textarea.value.trimStart();
}
}
You need to apply semaphore logic, like:
let beingClicked = false;
function clicks() {
beingClicked = true;
console.log('hi');
//I want to make the appear function stop working when this function is executing.
beingClicked = false;
}
function appear() {
//this is not working
if (beingClicked) {
message.style.display = 'none'
console.log('it works')
} else {
message.style.display = 'revert'
textarea.value = textarea.value.trimStart();
}
};
Note that at the start of the function, we set the semaphore (beingClicked) to red (which is true in this case) and at the end of the function we set it to false.
It is important to note that there are two further problems to consider:
You may need to reuse this logic for other buttons. If so, then you could consider the following:
function ButtonSemaphore(button) {
let semaphore = false;
let events = [];
button.addEventListener("click", function(evt) {
semaphore = true;
for (let event of events) event(evt);
semaphore = false;
});
function isRed() {
return semaphore;
}
function isGreen() {
return !semaphore;
}
function addEvent(callback) {
events.push(callback);
}
}
You can instantiate this by calling it as let myButtonSemaphore = new ButtonSemaphore(button), where button is a valid button. Whenever you want to add an event to it, just call myButtonSemaphore.addEvent(function(evt) {/* Some code */}). Note that the event array is processed and the semaphore is set properly before all events and after all events.
You may need to reuse this, so it is recommended to create a .js file of its own, so your code will exist exactly once and will be importable whenever needed.
let textarea = document.getElementsByTagName('textarea')[0]
let message = document.getElementById('message')
function clicks() {
console.log('hi')
//I want to make the appear function stop working when this function is executing.
}
function appear() {
//this is not working
if (document.getElementsByTagName('button')[0].onclick == true) {
message.style.display = 'none'
console.log('it works')
} else {
message.style.display = 'revert'
textarea.value = textarea.value.trimStart();
}
};
<textarea></textarea>
<div id="message">.....</div>
<button onclick="clicks()">split(20px)</button>