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

268
Visualizações
How do I allow function #2 to run only after function #1 is complete, but without triggering function #2?

I have 2 functions, onTouchStart and onTouchEnd, where onTouchEnd uses a state variable that onTouchStart sets. I'm worried that the onTouchEnd function could read the wrong value of a state variable because onTouchStart might not have finished setting the state. The main problem is that I don't want onTouchEnd to get triggered every time onTouchStart finishes, but need onTouchStart to set the state-stored variable to the correct value before onTouchEnd executes.

onTouchStart(){
   this.setState({
      randomBool: true   
   }); 
}

onTouchEnd(){
   console.log("randomBool should be true: " + this.state.randomBool);

   //then some stuff here
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use a check followed by setTimeout:

onTouchEnd(){
  if (!this.state.randomBool)
    setTimeout(this.onTouchEnd, 10);

  //then some stuff here
}

If needed you can also pass parameters into the "recursive" call: setTimeout(() => this.onTouchEnd(v1, v2), 10);

about 4 years ago · Juan Pablo Isaza Relatório

0

In order to do this deterministically, you need some state to which you can write synchronously, and you need something to indicate that setState has completed.

(I'm making an assumption here that setState provides this indication in the form of a promise).

In the snippet below, hold the mouse down for less than a second, and then try again for more than a second...

document.getElementById("touchMe").addEventListener('mousedown', onTouchStart);
document.getElementById("touchMe").addEventListener('mouseup', onTouchEnd);

let state;  // assuming we can only read and write this async...

// ...with this getter and setter
async function setState(object) {
  return new Promise(resolve => {
    setTimeout(() => {
      state = object
      resolve()
    }, 1000);
  })
}

async function getState() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(state)
    }, 1000);
  })
}

// BUT, we can write this synchronously
let settingState = false;

function onTouchStart() {
  settingState = true;
  setState({ someBool: true }).then(() => {
    settingState = false;
  })
}

function onTouchEnd() {
  if (settingState) {
    console.log("giving up, because we're busy writing state");
    return;
  }
  console.log("we can get state now");
  getState().then(state => console.log(state));
  
}
<div id="touchMe" style="background-color:yellow;">Touch Me</div>

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