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

251
Visualizações
How to stop a function on button click in react JS

I am calling a function from sort.jsx

<button onClick={() => onBubbleSort()}>Bubble sort</button>

and function definition is:

BubbleSort() {
    // console.log(this.state.animationSpeed)
    let bar = document.getElementsByClassName("arrayElement");
    let mySortedArray = bubble.bubblesort(this.state.array, bar, (arr) => {
      this.setState({ array: arr });
    },this.state.animationSpeed);
  }

Which is then calling a bubble sort function in another file bubbleSort.js

async function bubblesort(array1, element, setState, speed) {
  let compareSpeed = speed * 1.5;
  let swapSpeed = speed * 2;
  let isLessSpeed = speed * 3;
  let temp;
  let array = array1;
  document.getElementById("captions").innerText =
    "Lets's start sorting with Bubble sort!!!";
  await pauseIt();
  for (let i = 0; i < array.length - 1; i++) {
    for (let j = 0; j < array.length - 1 - i; j++) {
      await compare(element[j], element[j + 1], compareSpeed);
      if (array[j] > array[j + 1]) {
        await isLess(element[j], element[j + 1], isLessSpeed);
        temp = array[j];
        array[j] = array[j + 1];
        array[j + 1] = temp;
        await swap(element[j], element[j + 1], swapSpeed);
        unselect(element[j], element[j + 1],compareSpeed);
        setState(array);
      } else {
        unselect(element[j], element[j + 1],compareSpeed);
      }
    }
    let k = array.length - i - 1;
    element[k].style.backgroundColor = "green";
    if (speed >= 500) await success(i + 1);
  }
  element[0].style.backgroundColor = "green";
}

I want to stop the execution of this function on a buttoon click.

about 4 years ago · Juan Pablo Isaza
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