1<div class='container'>
2 Seconds : <div id='seconds'></div>
3</div>
4
5<!-- Script -->
6<script type='text/javascript'>
7
8 var count = 0;
9 var myInterval;
10 // Active
11 window.addEventListener('focus', startTimer);
12
13 // Inactive
14 window.addEventListener('blur', stopTimer);
15
16 function timerHandler() {
17 count++;
18 document.getElementById("seconds").innerHTML = count;
19 }
20
21 // Start timer
22 function startTimer() {
23 console.log('focus');
24 myInterval = window.setInterval(timerHandler, 1000);
25 }
26
27 // Stop timer
28 function stopTimer() {
29 window.clearInterval(myInterval);
30 }
31</script>