Search code examples
javascriptvimeovimeo-api

loadVideo() + setCurrentTime() in one onclick-Event


By clicking a button i want an embeded vimeo-player to:

  • change the played video to a given ID
  • jump to a given time
  • play the video

At first i tried: onclick="player.loadVideo(12345678); player.setCurrentTime(5); player.play()."

This obviously didn't work. I already learned that the loadVideo()-function fires an event - so the second function has to run after this event was caught. Since my understanding of JavaScript is still pretty limited i would love to learn how this would work.


Solution

  • as the previous mentioned about promise. All you need is to wait for the methods complete properly. Beside writing then and catch as the old way I can recommend async await way which is more readable. you can see as following

    const playVideo = async (id, time) => {
       try {
         await player.loadVideo(id);
         await player.setCurrentTime(time);
         await player.play();
       } catch (err) {
         console.log(err);
       } 
    }
    onclick="playVideo(309565369, 5)"