0

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 ?

aiddev
  • 1,409
  • 2
  • 24
  • 56
  • I am not entirely clear. However once the user reached 30% of video. Make the function execute only once using some flag variable. My assumption would be the function executes repeatedly ,once after 30% reached. – divakar Nov 04 '15 at 06:31
  • can you show me some example of using flag variable ? @divakar – aiddev Nov 04 '15 at 06:50
  • It can be anything. simply declare a variable like var a=0; – divakar Nov 04 '15 at 07:03

1 Answers1

2
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;
                        }
                    }); 
                }
            }  

    }
});
aiddev
  • 1,409
  • 2
  • 24
  • 56
divakar
  • 1,379
  • 6
  • 15
  • 31
  • again in this case my ajax calls multiple times @divakar – aiddev Nov 04 '15 at 07:20
  • please check this fiddle http://jsfiddle.net/4yb832q7/1, I modified a bit your code and changed place where you assigned 1 value to a, now ajax calls only three times, can you help me to understand why it calls 3times please? @divakar – aiddev Nov 04 '15 at 07:30
  • Maybe its due to, asynchronous not able to check the code in fiddle. just assign the variable in right place or assign variable where u calling this function. – divakar Nov 04 '15 at 08:31
  • no, I created fiddle just for showing you code, but I launched code in my live website, and ajax calls 3 times @divakar – aiddev Nov 04 '15 at 08:33
  • 1
    I updated you answer, with updated code I got desired result, thanks for giving idea of flag variables. – aiddev Nov 04 '15 at 09:33