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

181
Views
correctly synchronise function operations in a React hook

Consider the following code inside a React custom hook:

function createFadeOut() {
    if (!isDown && fading) {
      if (lineQueue.length !== 0)
        fadeOut(lineQueue.shift());
      else
        addLine(line => { return []; });
    }
    if (isDown && fading) {
      console.log('DOWN!');
    }
  }
  function fadeOut(line) {
      //code 
    }
  }


setInterval(createFadeOut, 60);

So what I want is a set of actions to happen in a discrete interval of time, depending on the state of the mouse and another event. isDown is set when mouse events are called. fading is a boolean state that is just called on a button click.

The specific behavior is that a list pops off an element and calls a function every 60 frames when the mouse is up, AND the list and other actions will be reset if the mouse is ever pressed again.

Of course, I am struggling correctly setting up the behavior.

  • isDown doesn't represent the mouse state at all when calling the function by a setInterval. Most of the time, it will always be set to true when I already left the mouse alone.
  • Using a useEffect hook, so it always represents the actual state of the mouse is problematic, it will be only called when a render happens and not at an interval.
  • Same problems if I just stick the function in the mouse events and use setTimeouts, especially due to mouseUp only being called on a few frames.
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!