Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger an event in javascript before a video has ended

I have a video that I fade in, play for ca. 25 seconds, fade out, wait 30 seconds and then fade in again. I use the following to trigger playback after video has ended:

$('.header-video').get(0).onended = function(e) {
  $(this).fadeOut(200);

  setTimeout(function(){
    playVideo();
  },8000);
}

Problem is that fading don't start until the video has actually stopped playback, so I wondered if there is some clever way I can get an event a few seconds in advance of the video ending? (probably not)

Alternatively I could just time the video and trigger a timed event, but maybe someone here can think of some cool wizardry that would enable me to do it in a more dynamic way than having to time the video manually.

like image 991
zkwsk Avatar asked Jul 30 '14 11:07

zkwsk


People also ask

What does the ended event mean in a video?

The ended event occurs when the audio/video has reached the end. This event is useful for messages like "thanks for listening", "thanks for watching", etc.

How to run JavaScript when a video has ended?

Run JavaScript when a video has ended using addEventListener('ended') Using JavaScript with Hype markus(Markus Bjerre) April 21, 2015, 9:22am #1

How to make a ‘next’ button appear after an event video finished?

I wanted a ‘next’ button to appear AFTER an event video finished playing. There might be a way of achieving this with the timeline, but I thought this method worked better. 1. Create a ‘next’ button and ID name it ‘nextBtn’. 2. Hide the button (press the eyeball) 3. Import your event video onto the frame (name does not matter) 4.

How do I jump to another scene when a video ends?

For others looking to perform an action (like jump to another scene) when a video has ended, here's a quick code snippet you can use: Set a unique element ID for your video in the Identity inspector: myVideo Next, run the following code 'on scene load' for the scene containing the video.


1 Answers

You can use the timeupdate event that fires whenever the video's currentTime attribute updates.

Reference: https://developer.mozilla.org/en-US/docs/Web/Events/timeupdate

Example:

$(document).ready(function(){

    $( '.header-video' ).on(
        'timeupdate', 
        function(event){

            // Save object in case you want to manipulate it more without calling the DOM
            $this = $(this);

            if( this.currentTime > ( this.duration - 3 ) ) {
                $this.fadeOut(200);
            }

        });

});

JSFiddle

like image 88
jjjjjjjjjjjjjjjjjjjj Avatar answered Oct 23 '22 16:10

jjjjjjjjjjjjjjjjjjjj