0

I am new to JQuery. I have searched previous posts but couldn't find anything. I am trying to do call another function from Video Ended Event handler. its not working.

$(document).ready(function(){
   // doing something
   $("#video1").bind("ended", NextFrag());
});

function NextFrag(){
   Window.alert("Hello World");
}

This is not working. I cannot see any alert printing "Hello World".

Basically I need to solve the above problem to do the following task. I want to play different fragments of video. The algorithm should be something like this:

$(document).ready(function(){
   NextFrag();
});

function NextFrag(){

   // IF First FRAGMENT do this
      $("#video1").html('<source src="FirstURLFromArray.mp4" type="video/mp4"></source>' );

    // ELSE DO THIS
      $("#video1").bind("ended", function(){
         $("#video1").html('<source src="NextURLFromArray.mp4" type="video/mp4"></source>' );
         NextFrag();    // call itself again.       
      });
}

Any help will be very appreciated. Thanks,

3
  • $( "#video1" ).bind( "ended", NextFrag ); Ditch the parens. You want to bind the function, not invoke it immediately. Commented Dec 25, 2012 at 19:31
  • The window is lowercase, won't work in camelcase. Commented Dec 25, 2012 at 19:37
  • Stupid me. You both are right. Many thanks. its working now. Commented Dec 25, 2012 at 19:49

3 Answers 3

3

Try

$("#video1").bind("ended", NextFrag);

This will pass the function as a callback, instead of evaluating it.

Sign up to request clarification or add additional context in comments.

2 Comments

Strictly speaking, the function will already have been evaluated at that point. A function invocation evaluates the function's body, but the function itself is evaluated when its function object is crated (in this case, when the function declaration is evaluated).
I just completed the task and posted the final code (oops!!!! I can't answer my own question for 8 hours - I will do it tomorrow). Many thanks for your help. I am not sure if you can help me get some points.
2
$(document).ready(function(){
   $("#video1").on("ended", function() {
    NextFrag();
   });
});

function NextFrag(){
   alert("Hello World");
}

1 Comment

I actually need to load first video before using the ended event handler. Many thanks for your reply.
0

Thanks for your help. I just completed the task. I did something like this and it is working.

var index = 0;
$(document).ready(function(){
NextFrag();
});  

function NextFrag(){
if (index < url.length){
    if(index == 0 )
    {
        //The only difference for index=0 is that the player is not autoplay  
        $("#video1").html('<source src= " '+ url[index] + '" type="video/mp4"></source>' );
        index++;
        $("#video1").bind( "ended", NextFrag);

    }else
    {
         $("#VideoContainer").html('<video  id="video1" controls autoplay > "<source src= "'+ url[index]+ '" type="video/mp4"></source> </video>' );
         index++;
         $("#video1").bind( "ended", NextFrag);
    } 
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.