请帮助我。我必须找到用户在页面上完成事件(如鼠标移动、按键等)的总时间。我想获得该页面已被使用的有用时间。 我编写了一些代码来计算页面打开的总时间以及用户关注该页面的时间。
计算此有用时间的另一种方法是计算页面上的总空闲时间,其中未执行任何操作(我的意思是鼠标移动、按键)。 有用时间=总页面打开时间-总空闲时间
请帮忙 这是代码。
var start, end, openingtime, pagefocustime = 0;
$(document).ready(function(){
start = performance.now();
$(window).on('blur', function() {
end = performance.now();
pagefocustime += end - start
})
$(window).on('focus', function() {
start = performance.now();
})
$(window).on('beforeunload',function(){
end = performance.now();
pagefocustime += end - start
console.log("Exact Page Focused Time : ");
console.log(pagefocustime);
openingtime = end-start;
console.log("Total Page Read Time : "+ openingtime);
});
});
请帮忙。 提前致谢。
最佳答案
这就是答案。将此代码与上述代码一起使用。
function setup() {
this.addEventListener("mousemove", resetTimer, false);
this.addEventListener("mousedown", resetTimer, false);
this.addEventListener("keypress", resetTimer, false);
this.addEventListener("DOMMouseScroll", resetTimer, false);
this.addEventListener("mousewheel", resetTimer, false);
this.addEventListener("touchmove", resetTimer, false);
this.addEventListener("MSPointerMove", resetTimer, false);
startTimer();
}
setup();
function startTimer() {
timeoutID = window.setTimeout(goInactive, 10000);
}
function resetTimer(e) {
window.clearTimeout(timeoutID);
goActive();
}
function goInactive() {
idletime = idletime + inactivetime;
}
function goActive() {
startTimer();
}
关于javascript - 计算用户在页面上完成事件的总时间,例如 jquery 中的 mousemove,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39639699/