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>
要下班了没时间了,这两个函数能帮到你第一个把时间转换为整数,这样你就能去计算了,第二个把数字转换成时间,你可以用来显示了
function getIntFromTime(time) {
time = time.split(':')
return parseInt(time[0], 10) * 60 * 60 + parseInt(time[1], 10) * 60 + parseInt(time[2], 10)
}
function getTimeFromInt(value) {
var h = Math.floor(value / 60 / 60)
var m = value % (60*60)
var s = value % (60)
if (h.toString().length <2) h = '0' + h.toString()
if (m.toString().length <2) m = '0' + m.toString()
if(s.toString().length <2) s = '0' + s.toString()
return h + ':' + m + ':' + s
}
div上绑定一个hover事件,并开始计时。以jquery为例。
var tid = 0$( "#div" ).hover( function() {
tid = setTimeout( function() {
//当触发hover就开始自动在1秒后执行相应代码
}, 1000 )
}, function() {
clearTimeout( tid )//当在1秒内退出了hover事件就取消计时代码
} )