<script>
;(function(){
var startTime = Math.ceil(new Date().getTime()/1000); //单位秒
var play_time = ""
//获取视频标签播发时间
$('#video-active').on("timeupdate",function(event){
var all_time = this.duration
play_time = this.currentTime
})
//初始化时间
getDuration = function(){
var time = '',
hours = 0,
minutes = 0,
seconds = 0,
endTime = Math.ceil(new Date().getTime()/1000),
duration = endTime - startTime;
if(play_time){
duration = play_time;
}
// var g_time = "";
var dt = localStorage.getItem("param");
if(dt == null || dt == "" || dt == undefined){
localStorage.setItem("param", duration);
// data = param;
}else{
duration = duration + parseInt(dt);
}
localStorage.setItem("param", duration);
hours = Math.floor(duration/3600); //停留小时数
minutes = Math.floor(duration%3600/60); //停留分钟数
seconds = Math.floor(duration%3600%60); //停留秒数
time = (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds < 10 ? '0' + seconds : seconds);
$.ajax({
type: "POST",
url: "url",
data: {time: time,id: id,play_time: play_time},
success: function(data) {
console.log(data);
},
error: function() {
}
});
};
//监听窗口关闭事件
window.onbeforeunload = function(e){
var duration = getDuration();
//request(duration);
};
})();
</script>