Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!