sagar chapagain sagar chapagain - 5 months ago 12
Javascript Question

perform an action on "timeupdate" after certain time interval

I am using html5 video and trying to store the amount of time video viewed by user for continuing the video from where it was closed.
For this I am trying to update the database every 5 second video was viewed I did this to get current time every 5 seconds:

$(document).ready(function()
{
$("video#eclassvideo").on(
"timeupdate",
function(e){
performaction(this.currentTime, this.duration);
});


function performaction(currentTime, duration){
setTimeout(function(){
console.log(currentTime);
console.log(' ajax action goes here')
},5000)
}


But this works only for first time it grabs after that it again continues to provide time 2-3 time per second. I tried using settimeout function during event firing but result was same. Please help me how do I get current time of video every 5 seconds only once.

Thanks in advance.

Answer

This solved my problem .Thanks

 $(document).ready(function(){
var player;     
var video='';
        $('#eclassvideo').bind('play',function(){
               video=document.getElementById('#eclassvideo');  
               var videotime=setInterval(function() {
               if(typeof($('#eclassvideo')[0])=='undefined'){
                        window.clearInterval( videotime); 
                        return false;
                }
                var curtime = $('#eclassvideo')[0].currentTime;
                 var duration = $('#eclassvideo')[0].duration;
                saveprogressvideo(curtime,duration);
                 }, 5000);   
               $('#eclassvideo').bind('ended',function(){
                        window.clearInterval(videotime);
                });
        });
 })

function saveprogressvideo(currenttime,duration)
{
    var timevideo=$('#eclassvideo').attr('timevideo');
    var data=$('.videocontent').data();     
    data.videolength=duration.toFixed(2);
    if(timevideo<currenttime)
    {
        $.ajax({
            url:'student_progress/learn_course/savevideotime',
            data:data,
            type:'post',
            dataType:'json',
            success:function(msg){
                $('.vidmsg').html(msg);
            }
        })
    } 
}
Comments