方式一
<script>
//初始化开始时间
var sTime = new Date();
//初始化结束始时间
var eTime = new Date();
window.addEventListener('pagehide',
function () {
eTime = new Date();
var sStr = generateTimeReqestNumber(sTime);
var eStr = generateTimeReqestNumber(eTime);
console.log(sStr);
console.log(eStr);
console.log(console.log(Math.floor((eTime - sTime) / 1000)));
$.post("http://fin.allinpaysc.com:18002/water/visit",function () { }, "json");
});
//计算带0的
function pad2(n) {
return n < 10 ? '0' + n : n;
}
//计算时间
function generateTimeReqestNumber(date) {
return date.getFullYear().toString() + pad2(date.getMonth() + 1) + pad2(date.getDate()) + pad2(date.getHours()) + pad2(date.getMinutes()) + pad2(date.getSeconds());
}
</script>
方式二
<script>
if (window.addEventListener) {
window.addEventListener("pagehide", function () { // IOS
$.post("http://slychat.yizhaosulianyun.com/Home/indexmodel?token=UYDpf4tcVp/5oqAfUrbaXA==", function () { }, "json");
console.log('页面要关闭了:pagehide');
})
} else {
window.onbeforeunload = function () { // 安卓
$.post("http://slychat.yizhaosulianyun.com/Home/indexmodel?token=UYDpf4tcVp/5oqAfUrbaXA==", function () { }, "json");
console.log('页面要关闭了:onbeforeunload');
}
}
</script>