在 onTime 函数 jwplayer 中只做 ajax 请求一次

do ajax request only once inside onTime function jwplayer

这是我的代码:

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){ 

                }
      });
    }      
});

我正在使用 wordpress 和 JW Player 7 javascript 版本并检测视频观看次数(当用户至少观看了 30% 的视频时)我正在调用 ajax 并在 'elapsedDurationPercentage' 操作我正在添加具有相应值的自定义字段并在下次更新。

现在在 onTime() 函数内部,我的 ajax 请求多次调用事件位置。如何在此函数中仅调用一次 ajax ?

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

    }
});