PDA

View Full Version : Far to complex for the noobie ?



scooby545
01-21-2009, 05:18 PM
This code is the combination of a working countdown script and a string splitter. Both scripts have been tested and work fine on thier own but when i try and merge them it all goes horribly wrong lol

The original countdown script used to update two text feilds minuets and seconds on a countdown from any given number of minuets.

The string splitter i am using to use pictures representing the numbers in the string i.e 0 = 0.jpg up to 9 = 9.jpg

can anyone see where im going wrong as the original coundown script is far more advanced than my javascript knowledge and my best guesses arent working lol

thanx in advance

scooby




<head>
<title>Countdown</title>
<script type="text/javascript">
// time string
var timestr = 0000;

// how many minutes
var mins = 3;

// how many seconds (don't change this)
var secs = mins * 60;
function countdown() {
setTimeout('Decrement()',1000);
}
function Decrement() {
var minutes = document.getElementById("minutes");
var seconds = document.getElementById("seconds");
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = String(secs).pad(2,0,'left');
} else {
minutes.value = String(getminutes()).pad(2,0,'left');
seconds.value = String(getseconds()).pad(2,0,'left');
}
secs--;
timestr = minutes.concat(seconds);
var start = 0;
var finish = 0;

for(var i = 0; i < timestr.length; i++){
start = (i);
finish = (i+1);
document.getElementById('sw1').innerHTML = "<img border='0' src='clock/" + timestr.substring(start,finish) + ".jpg'>";
start = (i-1);
finish = (i);
document.getElementById('sw2').innerHTML = "<img border='0' src='clock/" + timestr.substring(start,finish) + ".jpg'>";
start = (i-2);
finish = (i-1);
document.getElementById('sw3').innerHTML = "<img border='0' src='clock/" + timestr.substring(start,finish) + ".jpg'>";
start = (i-3);
finish = (i-2);
document.getElementById('sw4').innerHTML = "<img border='0' src='clock/" + timestr.substring(start,finish) + ".jpg'>";
}

if(secs != -1) setTimeout('Decrement()',1000);
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return mins;
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return secs-Math.round(mins *60);
}

//length (integer): length of resulting string,
//pad (string): to fill string with,
//type (string): where to pad, default is 'right'
String.prototype.pad = function(length, pad, type){
if(!String(pad).length || this.length >= length)return this.toString();
var string = this;
switch(type){
case 'left':
while(string.length < length)string = pad.toString() + string;
break;

default:
while(string.length < length) string += pad.toString();
}
return string;
}

</script>
</head>
<body onload="countdown();">

<table id='swtab'>
<tr>
<td><p id='sw4'></p>
</td>
<td><p id='sw3'></p>
</td>
<td><img border='0' src='clock/colon.jpg'></td>
<td><p id='sw2'></p>
</td>
<td><p id='sw1'></p>
</td>
</tr>
</table>

<input id="minutes" type="hidden">
<input id="seconds" type="hidden">

</body>