Skip to content

Instantly share code, notes, and snippets.

@ricogoh
Created November 10, 2017 11:07
Show Gist options
  • Save ricogoh/f36485de3c1e693798b63e047b54625e to your computer and use it in GitHub Desktop.
Save ricogoh/f36485de3c1e693798b63e047b54625e to your computer and use it in GitHub Desktop.
Detecting idle time in JavaScript elegantly // source https://jsbin.com/burabos
<html>
<header>
<meta name="description" content="Detecting idle time in JavaScript elegantly">
</header>
<body id="app">
<div id="demo"></div>
<p id="coor"></p>
<script id="jsbin-javascript">
document.getElementById("app").onmousemove = function(e) {resetTime(e)};
document.getElementById("app").onkeypress = function(e) {resetTime(e)};
var idleTime = 0;
var sec_do_something = 10;
var idleInterval = setInterval(timerIncrement, 1000);
function timerIncrement() {
idleTime = idleTime + 1;
document.getElementById("demo").innerHTML = idleTime + " seconds";
if (idleTime >= sec_do_something) {
idleTime = 0;
console.log('do something');
}
}
function resetTime(e) {
idleTime = 0;
var x = e.clientX;
var y = e.clientY;
var coor = "Coordinates: (" + x + "," + y + ")";
document.getElementById("coor").innerHTML = coor;
}
</script>
<script id="jsbin-source-html" type="text/html"><html>
<header>
<meta name="description" content="Detecting idle time in JavaScript elegantly">
</header>
<body id="app">
<div id="demo"></div>
<p id="coor"></p>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.1.0.js"><\/script>
</script>
<script id="jsbin-source-javascript" type="text/javascript">document.getElementById("app").onmousemove = function(e) {resetTime(e)};
document.getElementById("app").onkeypress = function(e) {resetTime(e)};
var idleTime = 0;
var sec_do_something = 10;
var idleInterval = setInterval(timerIncrement, 1000);
function timerIncrement() {
idleTime = idleTime + 1;
document.getElementById("demo").innerHTML = idleTime + " seconds";
if (idleTime >= sec_do_something) {
idleTime = 0;
console.log('do something');
}
}
function resetTime(e) {
idleTime = 0;
var x = e.clientX;
var y = e.clientY;
var coor = "Coordinates: (" + x + "," + y + ")";
document.getElementById("coor").innerHTML = coor;
}
</script></body>
</html>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
document.getElementById("app").onmousemove = function(e) {resetTime(e)};
document.getElementById("app").onkeypress = function(e) {resetTime(e)};
var idleTime = 0;
var sec_do_something = 10;
var idleInterval = setInterval(timerIncrement, 1000);
function timerIncrement() {
idleTime = idleTime + 1;
document.getElementById("demo").innerHTML = idleTime + " seconds";
if (idleTime >= sec_do_something) {
idleTime = 0;
console.log('do something');
}
}
function resetTime(e) {
idleTime = 0;
var x = e.clientX;
var y = e.clientY;
var coor = "Coordinates: (" + x + "," + y + ")";
document.getElementById("coor").innerHTML = coor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment