Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda