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

140
Visualizações
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 Respostas
Responde à pergunta

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 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