JavaScript——易班优课YOOC课群课程视频立刻完成解决方案

问题分析

当视频即将播放完成时,会发送AJAX

源代码


<script type="text/javascript">

            window.int = self.setInterval("nodrag()", 100)
            window.video_current_time = 0;
            window.count = -1;
            window.count_done = 0;
            window.ybx_api = 0;

            function nodrag() {
                var video = document.getElementsByClassName("nodrag")[0];
                if(!video){
                    return false;
                }
                if(!video.paused){
                    //易班熊看视频加积分接口
                    if(window.ybx_api<1){
                        window.ybx_api = 1;
                        $.get('/ybx_api', {}, function(data){
                            console.log('ybx add 10.');
                        });
                    }

                    count = count + 0.1;
                    var time = video.currentTime;
                    var duration = video.duration;
                    var ajaxurl = $("#video").attr("data-ajaxurl");
                    // if(window.count_done<3&&time){
                    //     $.ajax({
                    //         url: ajaxurl,
                    //         type: "POST",
                    //         data: {
                    //             "csrfmiddlewaretoken": "VDaWJ42VtTizoD02Oju7719a1TkQDlAH",
                    //             "saved_video_position": "00:00:01",
                    //             "video_duration": "00:00:02",
                    //             "done": true
                    //         }
                    //     });
                    //     count_done = 3;
                    // }
                    if(window.count_done<1&&(time/duration)>0.85){
                        $.ajax({
                            url: ajaxurl,
                            type: "POST",
                            data: {
                                "csrfmiddlewaretoken": "VDaWJ42VtTizoD02Oju7719a1TkQDlAH",
                                "saved_video_position": "00:00:01",
                                "video_duration": "00:00:02",
                                "done": true
                            }
                        });
                        count_done = count_done + 1;
                    }
                    if(window.count_done<2&&(time/duration)>0.9){
                        $.ajax({
                            url: ajaxurl,
                            type: "POST",
                            data: {
                                "csrfmiddlewaretoken": "VDaWJ42VtTizoD02Oju7719a1TkQDlAH",
                                "saved_video_position": "00:00:01",
                                "video_duration": "00:00:02",
                                "done": true
                            }
                        });
                        count_done = count_done + 1;
                    }
                    if(window.count_done<3&&(time/duration)>0.95){
                        $.ajax({
                            url: ajaxurl,
                            type: "POST",
                            data: {
                                "csrfmiddlewaretoken": "VDaWJ42VtTizoD02Oju7719a1TkQDlAH",
                                "saved_video_position": "00:00:01",
                                "video_duration": "00:00:02",
                                "done": true
                            }
                        });
                        count_done = count_done + 1;
                    }
                    // xc = (time - video_current_time) - count;
                    // if(xc>2||xc<-2){
                    //     video.pause();
                    //     video.currentTime = video_current_time + count - 1;
                    //     video.play();
                    // }else{
                    //     return false;
                    // }

                }else{
                    return false;
                }
            }
        </script>

 

解决方案

模拟发送完成请求 

                    var csrfmiddlewaretoken=/"csrfmiddlewaretoken": "(.*)",/.exec(document.body.outerHTML)[1];
                    var ajaxurl = $("#video").attr("data-ajaxurl");
                    $.ajax({
                        url: ajaxurl,
                        type: "POST",
                        data: {
                            "csrfmiddlewaretoken": csrfmiddlewaretoken,
                            "saved_video_position": "00:00:01",
                            "video_duration": "00:00:02",
                            "done": true
                        }
                    });

参考文章

 

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页