Browser window active code example
Example: Browser window active
<div class='container'>
Seconds : <div id='seconds'></div>
</div>
<!-- Script -->
<script type='text/javascript'>
var count = 0;
var myInterval;
window.addEventListener('focus', startTimer);
window.addEventListener('blur', stopTimer);
function timerHandler() {
count++;
document.getElementById("seconds").innerHTML = count;
}
function startTimer() {
console.log('focus');
myInterval = window.setInterval(timerHandler, 1000);
}
function stopTimer() {
window.clearInterval(myInterval);
}
</script>