Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda