...

View Full Version : add times to give total time



tsclan
12-15-2004, 05:17 AM
I posted before about the same code but this time the problem is differnt and therefore I made a new thread for it. So heres the problem.

I currently have this code that displays the total time when you enter in 3 times.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>48897</title>
</head>
<body>
<form onsubmit="makeTotal();return false;">
<table>
<thead>
<tr>
<th>Minutes</th>
<th>Seconds</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" onblur="makeTotal()"></td>
<td><input type="text" onblur="makeTotal()"></td>
</tr>
<tr>
<td><input type="text" onblur="makeTotal()"></td>
<td><input type="text" onblur="makeTotal()"></td>
</tr>
<tr>
<td><input type="text" onblur="makeTotal()"></td>
<td><input type="text" onblur="makeTotal()"></td>
</tr>
</tbody>
</table>

</form>

<p id="result">Default Value</p>



<script type="text/javascript">
var result = document.getElementById("result");
result.style.display = "none";

function makeTotal()
{
var list = document.getElementsByTagName("form")[0].getElementsByTagName("table")[0].getElementsByTagName("input");
var total = 0;

for(var i = 0; i < list.length; i++)
{
total += (i % 2) ? Number(list[i].value) : Number(list[i].value) * 60;
}

var time = new Date(total * 1000);

result.firstChild.nodeValue = time.getUTCHours() + ":" + time.getUTCMinutes() + ":" + time.getUTCSeconds();
result.style.display = "";
}
</script>

</body>
</html>

That worked fine untill i put it to practice where there more than 3 times to be entered ( 68 to be exact ) .
see it here http://www.eugamers.com/hl2dq/hl2dqupdate.php

What I noticed was that when the total time appeared all it said was Nan:Nan:Nan
I was then told thas was becuase it was reading in all the input boxes including the ones which werent related to the time to be calculated. I was then told to change the for loop to this



for(var i = 1; i < list.length; i+=2) {
total += Number(list[i++].value) + Number(list[i++].value) * 60;
}


Although that change cured the NAN error. It displayed the wrong total time. As when I added a second, a minute was added in the total time and when I added a minute, a second was added in the total time.

Since I have basically no javascipt knowledge, would you be able to show me where and how I would be able to fix the logical error in the new code

thanks again for your time and knowledge.

glenngv
12-15-2004, 05:49 AM
Try this:


var result = document.getElementById("result");
result.style.display = "none";
function makeTotal()
{
var f = document.forms[0];
var total = 0;
var fieldMin, fieldSec;
var ctr = 1;

while (typeof (fieldMin = f.elements["m"+ctr]) != "undefined" && typeof (fieldSec = f.elements["s"+ctr]) != "undefined")
{
total += (Number(fieldMin.value) * 60) + Number(fieldSec.value);
ctr++;
}

var time = new Date(total * 1000);

var mm = time.getUTCMinutes();
var ss = time.getUTCSeconds();
if (mm < 10) mm = "0" + mm;
if (ss < 10) ss = "0" + ss;

result.firstChild.nodeValue = time.getUTCHours() + ":" + mm + ":" + ss;
result.style.display = "";
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum