onfocus onblur countdown timer problem

Javascript coding ..

Moderators: macek, egami, gesf

woohoogf
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Dec 02, 2010 5:50 pm

onfocus onblur countdown timer problem

Postby woohoogf » Sat Jun 18, 2011 2:51 pm

Hello I'm newbie in javascript and worked hard today on this and couldn't find the solution.
Please help me.



<script>
if (/*@cc_on!@*/false) {
document.onfocusin = startB;
document.onfocusout = stopB;
} else {
window.onfocus = startB;
window.onblur = stopB;
}

var x = 1000;
var y = 1;

function startB() {
if (x !== 'ok') {
x = x - y;
document.form.num.value = x;
timeoutID=setTimeout("startB()", 1000);
}
if (x == 0) {
x = 'ok';
document.form.num.value = x;
}
}

function stopB() {
clearTimeout(timeoutID);
}
</script>
<body onLoad="startB()">
<form name="form">
<input name="num" size="10" readonly="readonly" type="text">
</form>


When I open this page it counts down with value 1/sec normally and when I change window/tab it stops, I again come back it's continues 1/sec, this is what I want, but I have a big problem --> WHEN I GO TO THIS LINK AND CHANGE WINDOW/TAB VERY QUICKLY, THAT IS STILL UNLOADED, IT STARTS COUNTING DOWN IN ONBLUR STATUS, AND WHEN I COME BACK TO ONFOCUS STATUS, THE FUNCTION IS READ TWICE AND STARTS COUNTING DOWN 1/0.5sec... DOUBLE.

Can anyone help me with this?
Thanks in advance.

User avatar
gandahar
New php-forum User
New php-forum User
Posts: 112
Joined: Tue Mar 15, 2011 4:05 am

Re: onfocus onblur countdown timer problem

Postby gandahar » Wed Jul 06, 2011 10:28 am

you can enable clicks on tabs only when document.body is loaded, like this, no quick tab change..


Return to “JavaScript”

Who is online

Users browsing this forum: No registered users and 0 guests

cron