Forums

Forums (http://www.abandonia.com/vbullet/index.php)
-   Programming (http://www.abandonia.com/vbullet/forumdisplay.php?f=25)
-   -   Something weird with JavaScript (http://www.abandonia.com/vbullet/showthread.php?t=20715)

The Fifth Horseman 15-07-2009 01:46 PM

Something weird with JavaScript
 
This is the code:
HTML Code:

<title>
&nbsp
</title>
<SCRIPT LANGUAGE="JAVASCRIPT">
td = new Date();
BD = new Date("October 1, 2008");
ms = 24 * 60 * 60 * 1000 ;
tL = (BD.getTime() - td.getTime());
edL = tL / ms;
dL = Math.floor(edL);
ehL = (edL - dL)*24;
hL = Math.floor(ehL);
emL = (ehL - hL)*60;
mL = Math.floor(emL);
sL = Math.floor ((emL - mL)*60);
</script>
</head>
<BODY BGCOLOR=black TEXT=white>
<center>
<FONT FACE=Arial,Helvetica SIZE=10>
<script language="javascript">
if (self==parent)
{
document.write('<FONT SIZE=5><i><p>"Gantz."</p> <p>"Katastrophe."</p></i><BR><BR><BR></FONT>')
};
document.write("<B>"+dL+" d, "+hL+" hrs, "+mL+" min "+sL+" s</B>");
timerID = setTimeout("location.reload()",1000);
timerID = setTimeout("history.go(0)",1000);
</script>
</FONT>
</center>
</body>
</html>

The live page: http://gantz.katastrophe.w.interia.pl/details.html is showing the clock, the even it indicated has long since passed so it should be increasing it's count.
However, instead of doing that, the numbers decrease.
Where is the error in the script?

El Quia 15-07-2009 03:03 PM

Unless I am way too sleep-deprived for rational thinking, I think that I found your problem.

Look at this line:

Code:

tL = (BD.getTime() - td.getTime());
Because now td is bigger than BD, whe are having a negative number into tL. Now look:
Code:

tL = (BD.getTime() - td.getTime());
edL = tL / ms;
dL = Math.floor(edL);

The negative tL is divided by ms, maintaining the negativity and (here's the problem) you use Math.floor() on it and assign it to dL. Math.floor() rounds DOWNWARD to the nearest integer. That means that the absolute value (without taking into account the minus sign) the number get bigger. That's it, -287.4564634 turns into -288. And 288 > 287.4564634.

Then you do:
Code:

ehL = (edL - dL)*24;
hL = Math.floor(ehL);

On the first line, you substract a negative number from a smaller negative number, giving you a positive number that you multiply by 24. And from here, the problem introduced with dL = Math.floor(edL) propagates to the rest of the code, resulting in a countdown going down in absolute terms but going down if you consider the minus sign that is present at the beginning, because will always be bigger or equal to edL in absolute terms.

This also means that the time it says is inaccurate. I would try to fix it doing something like evaluating if tL is negative, change this line:
Code:

edL = tL / ms;
into:
Code:

edL = Math.abs(tL / ms);
and, when you will show the total time on
Code:

document.write("<B>"+dL+" d, "+hL+" hrs, "+mL+" min "+sL+" s</B>");
maybe puting a "-" if tL is negative or something. Or doing what I did:
Code:

<title>
&nbsp
</title>
<SCRIPT LANGUAGE="JAVASCRIPT">
td = new Date();
BD = new Date("October 1, 2008");
ms = 24 * 60 * 60 * 1000 ;
tL = (BD.getTime() - td.getTime());
edL = Math.abs(tL / ms);
dL = Math.floor(edL);
ehL = (edL - dL)*24;
hL = Math.floor(ehL);
emL = (ehL - hL)*60;
mL = Math.floor(emL);
sL = Math.floor ((emL - mL)*60);
</script>
</head>
<BODY BGCOLOR=black TEXT=white>
<center>
<FONT FACE=Arial,Helvetica SIZE=10>
<script language="javascript">
if (self==parent)
{
document.write('<FONT SIZE=5><i><p>"Gantz."</p> <p>"Katastrophe."</p></i><BR><BR><BR></FONT>')
};
if (tL < 0) {
document.write("<B>-"+dL+" d, "+hL+" hrs, "+mL+" min "+sL+" s</B>");
} else {
document.write("<B>"+dL+" d, "+hL+" hrs, "+mL+" min "+sL+" s</B>");
}
timerID = setTimeout("location.reload()",1000);
timerID = setTimeout("history.go(0)",1000);
</script>
</FONT>
</center>
</body>
</html>

I hope it helps you!

_r.u.s.s. 15-07-2009 08:08 PM

holy hell that one looks horrible, why would it refresh the whole page every time?
get another one
http://www.google.com/search?ie=UTF-...vascript+timer


The current time is 04:16 PM (GMT)

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.