Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Make a function stop working when a button is clicked(javascrpt)

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>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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();
        }
    }
about 4 years ago · Juan Pablo Isaza Report

0

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:

1

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.

2

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.

about 4 years ago · Juan Pablo Isaza Report

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!