On lost focus

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

On lost focus

Post by fastfingertips »

I have a countdown running on a page, and I want if the page is losing the focus to stop this countdown and when the focus is received again to start again but not from initial moment. Can you give me please a suggestion?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Something like this might work...

Code: Select all

<html>
<head>

<script type="text/javascript">

timeToWait = 10; // Seconds
timePassed = 0;

function StartTimer() &#123; timerINT = setInterval("RunTimer()", 1000); &#125;

function StopTimer() &#123; clearInterval(timerINT); &#125;

function RunTimer()
&#123;
     timePassed ++
     if(timePassed == timeToWait)
     &#123;
          StopTimer();
          alert("GAME OVER");
     &#125;
&#125;

</script>

</head>
<body onload="StartTimer()" onblur="StopTimer()">

Some text

</body>
</html>
Post Reply