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

105
Visualizações
React Native run function to increase variable while button is pressed

I have an api that decrease sound volume when a request is made. What I am looking to do is have the sound decrease when button is pressed in. The volume should continually decrease as the button is pressed in. When the button is release the sound volume will stop decreasing. The following code works however its not working smoothly. How can I make my function call to continually run into button is released.

const [volume, setVolume] = useState(0);

//  needs to run until button is released
useEffect(() => {
  let source = axios.CancelToken.source();

  axios.get(`${network}/volume=${volume}`, {
    cancelToken: source.token,
  });

  return function () {
    source.cancel("Cancelling in cleanup");
  };
}, [volume]);

const _handlePressIn = () => {
  timer.current = setInterval(() => setVolume((vol) => vol - 2), 60);
};

const _handlePressOut = () => {
  // stop setInterval so that volume does not increase
  clearInterval(timer.current);
};

<Pressable
  onPress={() => console.log("pressed")}
  onPressIn={() => _handlePressIn()}
  onPressOut={() => _handlePressOut()}
>
  <Text>Volume Up</Text>
</Pressable>;

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