Search code examples
javascriptajaxwordpressjwplayerjwplayer7

do ajax request only once inside onTime function jwplayer


Here is my code:

jwplayer("mySingleVideoWrapper").onTime(function (event) {
        jQuery("#getCurrentPositionOfVideo").val(event.position);
        var videoLength = jwplayer('mySingleVideoWrapper').getDuration();   
        jQuery("#getWholeDuration").val(videoLength); 
        var totalSec = videoLength;
        var finalNumber = (totalSec*30) / 100; 
        if (event.position > finalNumber) {
            jQuery("#watchedElapsedCount").val('1');
            var videoViewsCount = jQuery("#watchedElapsedCount").val();
            var currentPostId = jQuery("#currentPostId").val();
            var json = '{"videoViewsCount" : "'+ videoViewsCount +'", "currentPostId" : "'+ currentPostId +'"}';
            var getSiteBaseUrl = jQuery('#getSiteBaseUrl').val();
            var ajaxurl = getSiteBaseUrl+"/wp-admin/admin-ajax.php";
            jQuery.ajax({               
                url: ajaxurl,               
                type:"POST",                
                dataType: "json",
                data:{
                    action: "elapsedDurationPercentage",                    
                    data: json              
                },
                success:function(data){ 

                }
      });
    }      
});

I'm using wordpress and JW Player 7 javascript version and for detecting video views (when user has watched at least 30% of video) I'm calling ajax and in 'elapsedDurationPercentage' action I'm adding custom field with corresponding value and updating next time.

Now inside of onTime() function my ajax request is calling many times re event position. How can I call ajax only once inside of this function ?


Solution

  • var flag = 0;
        jwplayer("mySingleVideoWrapper").onTime(function (event) {
            if(flag == 0){
                jQuery("#getCurrentPositionOfVideo").val(event.position);
                var videoLength = jwplayer('mySingleVideoWrapper').getDuration();   
                jQuery("#getWholeDuration").val(videoLength); 
                var totalSec = videoLength;
                var finalNumber = (totalSec*30) / 100; 
                if (event.position > finalNumber) {
                    jQuery("#watchedElapsedCount").val('1');
                    var videoViewsCount = jQuery("#watchedElapsedCount").val();
                    var currentPostId = jQuery("#currentPostId").val();
                    var json = '{"videoViewsCount" : "'+ videoViewsCount +'", "currentPostId" : "'+ currentPostId +'"}';
                    var getSiteBaseUrl = jQuery('#getSiteBaseUrl').val();
                    var ajaxurl = getSiteBaseUrl+"/wp-admin/admin-ajax.php";
                    if(jQuery("#watchedElapsedCount").val() == '1'){
                        flag = 1;
                        var xhr = jQuery.ajax({               
                            url: ajaxurl,               
                            type:"POST",                
                            dataType: "json",
                            data:{
                                action: "elapsedDurationPercentage",                    
                                data: json              
                            },
                            success:function(data){  
                                flag = 1;
                                return flag;
                            }
                        }); 
                    }
                }  
    
        }
    });