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

139
Views
How to fire off a function at start of a video and a different function when the user pauses and presses play to continue watching the video?

Currently, I have a video.js player with React. My goal is to fire off a function when the user starts playing the video and to fire a separate function when the user plays the video back again after pausing the video(let's call this the "continue" event). How can I achieve this without firing the incorrect function at start and continue?

I've looked at several different HTML media events such as 'play' and 'playing', however the "continue" function is still firing at start.

Here's what I have:

const VideoPlayer = ({ handleOnPlayContinue, handleOnStart }) => {
  const [hasStarted, setHasStarted] = useState(false);
  
  useEffect(() => {
      videoJSPlayer.on('play'), () => {
        setHasStarted(true);
      };
      videoJSPlayer.on('playing'), () => {
        handleOnPlayContinue();
      };
    }, [videoJSPlayer, handleOnPlayContinue]);
  
  useEffect(() => {
    if (hasStarted) {
      handleOnStart();
    }
  }, [hasStarted, handleOnStart]);
}

Should I be toggling another boolean state on 'pause' and use that boolean flag to fire my continue function? I thought of setting another state to manage when the user is continuing the video after pausing, but wanted to know if there's another HTML media event that I can leverage.

Thanks for any tips and answers you may have!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I don't believe there is a relevant HTML media event; you are going to have to manage it yourself with state and within just the play event. See if the below works for what you are trying to achieve (I have not been able to test it). Keep in mind that changes to state in useEffect aren't updated synchronously, and hasStarted needs to be in the dependency array otherwise the event will reference stale state variables. We also want to clean up event listeners between useEffect calls.

const VideoPlayer = ({ handleOnPlayContinue, handleOnStart }) => {
  const [hasStarted, setHasStarted] = useState(false);

  useEffect(() => {
    videoJSPlayer.on("play", () => {
      if (!hasStarted) {
        handleOnStart();
        setHasStarted(true);
      } else {
        handleOnPlayContinue();
      }
    });
    return () => {
      videoJSPlayer.off("play");
    };
  }, [hasStarted, handleOnPlayContinue, handleOnStart, videoJSPlayer]);
};

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