Skip to content

Instantly share code, notes, and snippets.

@binary4cat
Created April 26, 2018 07:18
Show Gist options
  • Save binary4cat/6b6ed54dad57980fb13e99249227faf5 to your computer and use it in GitHub Desktop.
Save binary4cat/6b6ed54dad57980fb13e99249227faf5 to your computer and use it in GitHub Desktop.
js判断用户有没有操作页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js判断用户有没有操作页面</title>
</head>
<body>
<script>
window.onload = function (){
(function($){
funObj = {
timeUserFun:'timeUserFun',
}
$[funObj.timeUserFun] = function(time){
var time = time || 2;
var userTime = time*60;
var objTime = {
init:0,
time:function(){
objTime.init += 1;
if(objTime.init == userTime){
console.log(111) // 用户到达未操作事件 做一些处理
}
},
eventFun:function(){
clearInterval(testUser);
objTime.init = 0;
testUser = setInterval(objTime.time,1000);
}
}
var testUser = setInterval(objTime.time,1000);
var body = document.querySelector('html');
body.addEventListener("click",objTime.eventFun);
body.addEventListener("keydown",objTime.eventFun);
body.addEventListener("mousemove",objTime.eventFun);
body.addEventListener("mousewheel",objTime.eventFun);
}
})(window)
// 直接调用 参数代表分钟数,可以有一位小数;
timeUserFun(0.1);
}
</script>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js判断用户有没有操作页面</title>
</head>
<body>
<script>
window.onload = function (){
(function($){
funObj = {
timeUserFun:'timeUserFun',
}
$[funObj.timeUserFun] = function(time){
var time = time || 2;
var userTime = time*60;
var objTime = {
init:0,
time:function(){
objTime.init += 1;
if(objTime.init == userTime){
console.log(111) // 用户到达未操作事件 做一些处理
}
},
eventFun:function(){
clearInterval(testUser);
objTime.init = 0;
testUser = setInterval(objTime.time,1000);
}
}
var testUser = setInterval(objTime.time,1000);
var body = document.querySelector('html');
body.addEventListener("click",objTime.eventFun);
body.addEventListener("keydown",objTime.eventFun);
body.addEventListener("mousemove",objTime.eventFun);
body.addEventListener("mousewheel",objTime.eventFun);
}
})(window)
// 直接调用 参数代表分钟数,可以有一位小数;
timeUserFun(0.1);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment