PDA

View Full Version : It wont show in the form


Razzar
11-13-2002, 03:48 AM
I'm new to this so please dont hammer me down. But on with it.

I've managed to melt two different javascripts into one script. The script counts the passed full days (from a fixed past date) and converts them into seconds, plus it counts the seconds passed of the current day. Now to my problem:

I want to add these two numbers showed here (http://www.razzar.ishoejby.dk/javascripts/javastads.html) and make the total increment by one and show them in a text form, but I cant get it right :(. Please help. Thanks.

Regards,
Razzar out...

glenngv
11-13-2002, 05:44 AM
if i understand you correctly...

var difference=0;

function countup(yr,m,d){
//...
difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*86400);
document.write(difference+" seconds");
}

function startcount(){
//...
document.form1.text1.value=nTotal+difference;
setTimeout('startcount()', 1000);
}

Razzar
11-13-2002, 12:17 PM
...how do I pass on difference from the first function to the second?, coz it says that difference is not defined.

Regards,
Razzar out...

glenngv
11-14-2002, 01:22 AM
you must declare difference as a global variable, which I did!

var difference=0;

function countup(yr,m,d){
//...
}

function startcount(){
//...
}

Razzar
11-14-2002, 06:18 AM
Thanks for helping out, I really appreciate it.

I dont get any errors, with the newly declared variable, but it still doesnt add the numbers together and displays the total in the form. Please help.

Regards,
Razzar out...

glenngv
11-14-2002, 06:33 AM
can you post the updated link or code?

Razzar
11-14-2002, 06:44 AM
Here (http://www.razzar.ishoejby.dk/javascripts/javastads.html)

Regards,
Razzar out...

glenngv
11-14-2002, 06:47 AM
it is still the old code!

Razzar
11-14-2002, 07:09 AM
It works, it's just me screwing up :p , I'm sitting here with a 100 different things. Thanks man.

If its not too much to ask I have one more question.

The setTimeout function increments the value by 1 every 1000ms, how can I make it increment at a lower ms?, say 500ms or lower, I've tried 500ms, and it doesnt work, it only works if it's above 1000ms (2000, 3000ms etc).

Regards,
Razzar out.....

glenngv
11-14-2002, 08:07 AM
dont include ms
setTimeout('startcount()', 500);

Razzar
11-14-2002, 08:46 AM
This should make it increment 10 times every second but it doesnt, it still increments like it was set to 1000:

setTimeout('startcount()', 100);

Check it out here (http://www.razzar.ishoejby.dk/javascripts/javastads.html) its set at 100.


Regards,
Razzar out.....

glenngv
11-14-2002, 09:27 AM
maybe because it takes time to set nTotal variable.
i did a test by just setting nTotal to increment to 1 and it displays the value with the correct duration as expected.

Razzar
11-14-2002, 09:33 AM
How can I get it to increment every 500ms instead of 1000ms ?

Regards,
Razzar out...

glenngv
11-14-2002, 10:03 AM
copy and paste the whole code below.
it's set to 100 ms, just change to 500 as desired


<SCRIPT language="JavaScript">
var nTotal=0;
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countup(yr,m,d){
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()

var todaystring=montharray[todaym]+" "+todayd+", "+todayy
var paststring=montharray[m-1]+" "+d+", "+yr
var difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*86400);
document.write(difference+" seconds");

var nHours=today.getHours();
var nMinutes=today.getMinutes();
var nSecs=today.getSeconds();

var nHourtosecs=nHours * 3600
var nMinutestosecs=nMinutes * 60
nTotal=nHourtosecs + nMinutestosecs + nSecs + difference;


}

function startcount()
{
nTotal++;
if (nTotal==86400) nTotal=0;

document.form1.text1.value=nTotal;

setTimeout('startcount()', 100);

}

countup(2002,11,08)

</SCRIPT>


as you noticed, I changed back the difference variable from global to local and I declared nTotal as global.
and you can use getFullYear() instead of getYear() to get 4-digit year.

Razzar
11-14-2002, 08:19 PM
It works flawlessly, thanks mate. If you ever need something done in HTML, ill do it in return, HTML I know ;). L8rz.

Regards,
Razzar out...

Razzar
11-14-2002, 08:30 PM
Just noticed something, the counter seems to go back, if I leave the site and come back right after :confused:.

Regards,
Razzar out...

whammy
11-15-2002, 01:32 AM
I think you are imagining things? Unless you reset your computer clock.

Owl
11-15-2002, 04:33 AM
Sorry whammy, Razzar is not imagining things.

If you want to realize it yourself, activate the page
with todays date.
If the location you state by your name is correct
(41° 8' 52" N -95° 53' 31" W) then your time now
is about 10:30 pm and if you let the counter work
then it will be reset within less then 10 minutes.
In any event, no matter what time you enter the page
(with date of the same day), it will be reset within less
then 2 hours and 24 minutes.

( •) (• )
>>V

whammy
11-16-2002, 12:43 AM
Hmm... you're right. Must be a flaw in glenngv's code! (that's unusual!)

This works to display the correct time, maybe you can modify this to help:


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>showtime()</title>
<script type="text/javascript">
<!--
function showtime(){
var meridian="AM";
var mydate=new Date();
var myhours=mydate.getHours();
var myminutes=mydate.getMinutes();
var myseconds=mydate.getSeconds();
if (myhours>11){
myhours-=12;
meridian="PM";
}
if(myhours==0){
myhours=12;
}
if(myminutes<10){
myminutes="0"+myminutes;
}
if(myseconds<10){
myseconds="0"+myseconds;
}
mynewtime=myhours+":"+myminutes+":"+myseconds+" "+meridian;
var thefield=document.forms[0].thetime;
thefield.value=mynewtime
thefield.blur();
setTimeout("showtime()",1000)
}
//-->
</script>
</head>
<body onload="showtime()">
<form id="timeform">
<input type="text" name="thetime" size="11" maxlength="11"
style="background:#cccccc; color:#000000; border style:1px solid #666666; text-align:center; font-weight:normal; font-size:12px"
>
</input>
</form>
</body>
</html>