onFocus onBlur timer countdown problem

onFocus onBlur timer countdown problem

w00h00w00h00 Posts: 3Questions: 0Answers: 0
edited June 2011 in General
Hello I'm newbie in javascript and worked hard today on this and couldn't find the solution.
Please help me.




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);
}







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.

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    How does this relate to DataTables? This forum is for questions about the software presented on this site - there are better places to go for general Javascript help.

    Allan
  • w00h00w00h00 Posts: 3Questions: 0Answers: 0
    Sorry, I didn't know, please guide me to some good place.
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    http://stackoverflow.com/
  • w00h00w00h00 Posts: 3Questions: 0Answers: 0
    lol, thanks. Wrote there, too yesterday.
    3 answers but wrong.
This discussion has been closed.