Answers for "Browser window active"

0

Browser window active

<div class='container'>
 Seconds : <div id='seconds'></div>
</div>

<!-- Script -->
<script type='text/javascript'>
 
 var count = 0;
 var myInterval;
 // Active
 window.addEventListener('focus', startTimer);

 // Inactive
 window.addEventListener('blur', stopTimer);

 function timerHandler() {
  count++;
  document.getElementById("seconds").innerHTML = count;
 }

 // Start timer
 function startTimer() {
  console.log('focus');
  myInterval = window.setInterval(timerHandler, 1000);
 }

 // Stop timer
 function stopTimer() {
  window.clearInterval(myInterval);
 }
</script>
Posted by: Guest on May-17-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language