/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Missing something in logic — can you see it?

I am using the following test script to track the mouse position for a maximum of 30 seconds from the time the ‘Start’ button is clicked.

All is going fine for the test except I am not getting an X:Y entry for the very first second.
Can someone spot the logic I’m missing. Thanks for any ideas. ?

[code=php]
<html>
<head>
<SCRIPT TYPE=”text/javascript”>

// Description: Countdown timer (seconds) from ‘Start’ to “MaxSecs”
// NOTE: the month entered must be one less than current month. ie; 0=January, 11=December
// NOTE: the hour is in 24 hour format. 0=12am, 15=3pm etc
// format: Future = new Date(year,month-1,day,hour,min,sec)
// example: Future = new Date(2007,10,26,16,15,00) = Nov. 26, 2007 – 4:15:00 pm

var ShowCoords = false;
var MaxSecs = 30;
var ND = new Date();
var Future = new Date(ND.getFullYear(),ND.getMonth(),ND.getDate(),ND.getHours(),
(ND.getMinutes()+1),ND.getSeconds());
// Future = new Date(2007,10,4,19,58,0);

function SetCount(){
// if (ShowCoords == false) { return; } // bypass checks
ND = new Date();
Future = new Date(ND.getFullYear(),ND.getMonth(),ND.getDate(),ND.getHours(),
(ND.getMinutes()),(ND.getSeconds()+MaxSecs));
document.getElementById(‘testbox’).innerHTML=Future;
setTimeout(“GetCount()”, 1000);
}

function GetCount() {
if (ShowCoords == false) { return } // skip checks
Now = new Date(); //grab current date
amount = Future.getTime() – Now.getTime(); //calc milliseconds between dates
delete Now;

if(amount < 0){ // time is already past
document.getElementById(‘timeLeft’).innerHTML=”Now!”;
}
else{ // date is still good
days=0;hours=0;mins=0;secs=0;out=””;

amount = Math.floor(amount/1000);//kill the “milliseconds” so just secs
days=Math.floor(amount/86400);//days
amount=amount%86400;
hours=Math.floor(amount/3600);//hours
amount=amount%3600;
mins=Math.floor(amount/60);//minutes
amount=amount%60;
secs=Math.floor(amount);//seconds

if(days != 0){out += days +” day”+((days!=1)?”s”:””)+”, “;}
if(days != 0 || hours != 0){out += hours +” hour”+((hours!=1)?”s”:””)+”, “;}
if(days != 0 || hours != 0 || mins != 0){out += mins +” minute”+((mins!=1)?”s”:””)+”, “;}
out += secs;
// document.getElementById(‘timeLeft’).innerHTML=out+” seconds”;
document.getElementById(‘timeLeft’).innerHTML= out;

var tmp = ”;
var tleft = MaxSecs-secs;
// if (tleft != MaxSecs) {
tmp = document.getElementById(‘boxinfo’).innerHTML;
if (tleft % 5 == 0) { tmp += ‘<br />’; }
document.getElementById(‘boxinfo’).innerHTML
= tmp+tleft+’:’+document.getElementById(‘mousepos’).value+’| ‘;
// }
setTimeout(“GetCount()”, 1000);
}
}

// from: http://www.dynamic-tools.net/toolbox/getMousePosition/
function getMousePosition(e) {
return e.pageX ? {‘x’:e.pageX, ‘y’:e.pageY} :
{‘x’:e.clientX + document.documentElement.scrollLeft + document.body.scrollLeft,
‘y’:e.clientY + document.documentElement.scrollTop + document.body.scrollTop};
};
function showMousePos(e) {
if (ShowCoords == false) { return; } // skip checks
if (!e) e = event; // make sure we have a reference to the event
var input = document.getElementById(‘mousepos’);
var mp = getMousePosition(e);
input.value = ‘x:’ + mp.x + ‘:y:’ + mp.y;
};
function init() {
ShowCoords = false;
document.onmousemove = showMousePos;
};
window.onload = init;

</script>
</head>
<body>
<button onClick=”document.getElementById(‘boxinfo’).innerHTML=”;”>Clear</button>
<input type=”text” id=”mousepos” value=” size=”10″>
Counter:
<button onClick=”SetCount();GetCount();ShowCoords=true”>Start</button>
<button onClick=”ShowCoords=false”>Stop</button>
<br />
<div id=”testbox”></div>
<div id=”timeLeft”></div>

<div id=”boxinfo”></div>
</body>
</html>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorNov 06.2007 — Your simplified construction looks like:
<i>
</i>&lt;script type="text/javascript"&gt;
var ShowCoords=false;
function setCount(){
setTimeout(function(){getCount(1)}, 1000);
}
function getCount(i){
if(!ShowCoords){return}
alert(i)
}
onload=function(){
setCount();
getCount(0);
ShowCoords=true;
}
&lt;/script&gt;

You see now that in the anonymous the function getCount() is triggered [I]before[/I] the global variable is set to true, thus the getCount() will return nothing first time and it will be called again only by the setTimeout('getCount()',1000), that means [I]after 1 second[/I], thus you miss the first second position...

Try switch the order like this:
<i>
</i>&lt;button onClick="SetCount();ShowCoords=true;GetCount();"&gt;Start&lt;/button&gt;
Copy linkTweet thisAlerts:
@JMRKERauthorNov 06.2007 — Thanks 'Kor'. That was it.

Appreciate the look and explanation. ?
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...