The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> onfocus onblur contdown timer problem, onfocus onblur contdown timer problem
w00h00
post Jun 18 2011, 05:45 PM
Post #1





Group: Members
Posts: 2
Joined: 18-June 11
Member No.: 14,794



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 is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 18 2011, 07:31 PM
Post #2


.
********

Group: WDG Moderators
Posts: 8,826
Joined: 10-August 06
Member No.: 7



QUOTE(w00h00 @ Jun 19 2011, 12:45 AM) *

THE FUNCTION IS READ TWICE AND STARTS COUNTING DOWN 1/0.5sec... DOUBLE.

In which browser(s)? I saw something like that in IE9 even with a single tab, but it doesn't seem to happen if I remove the onload, onfocusin and onfocusout events:

CODE
<script type="text/javascript">
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>
<form name="form">
<input name="num" size="10" readonly="readonly" type="text">
</form>


But even with the above script I also saw it once in Chrome and once in Safari when opening two identical tabs. unsure.gif


User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jun 18 2011, 07:42 PM
Post #3


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 19,773
Joined: 9-August 06
Member No.: 6



For me it never stopped counting. IE7 and K-Mel.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 19 2011, 04:17 AM
Post #4


.
********

Group: WDG Moderators
Posts: 8,826
Joined: 10-August 06
Member No.: 7



QUOTE(pandy @ Jun 19 2011, 02:42 AM) *

For me it never stopped counting.

My version or the OP's? Did it go below zero? You mean it didn't stop when the page lost focus?

QUOTE
IE7 and K-Mel.

Now I noticed that it (at least my version) doesn't even start in IE7 (using the IETester emulator), while my K-Mel counts two seconds at the time even with a single tab.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
w00h00
post Jun 19 2011, 05:33 AM
Post #5





Group: Members
Posts: 2
Joined: 18-June 11
Member No.: 14,794



I need this to work even only with firefox and chrome.

Just need something to work like I want smile.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jun 19 2011, 06:01 AM
Post #6


.
********

Group: WDG Moderators
Posts: 8,826
Joined: 10-August 06
Member No.: 7



QUOTE(Christian J @ Jun 19 2011, 11:17 AM) *

K-Mel counts two seconds at the time even with a single tab.

When I printed all x values on the page, it seems the function is called twice by the window.onfocus event (when I removed window.onfocus and used a button with onclick to start the function it worked correctly in K-Meleon). Could it be a case of event bubbling?

BTW, K-Meleon uses an older version of Gecko (the same engine also used by Firefox).
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jun 19 2011, 09:07 AM
Post #7


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 19,773
Joined: 9-August 06
Member No.: 6



QUOTE(Christian J @ Jun 19 2011, 11:17 AM) *

QUOTE(pandy @ Jun 19 2011, 02:42 AM) *

For me it never stopped counting.

My version or the OP's? Did it go below zero? You mean it didn't stop when the page lost focus?


The original version. Yes, it never stops.

QUOTE
QUOTE
IE7 and K-Mel.

Now I noticed that it (at least my version) doesn't even start in IE7 (using the IETester emulator), while my K-Mel counts two seconds at the time even with a single tab.


It starts all right (the real thing). I don't use tabs, but I don't see that that would be an issue.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 25th July 2021 - 11:14 PM