...

View Full Version : using the value of a timer.. please help



cat_evilness
01-12-2003, 09:32 PM
Right I this is really important and I have no idea of how to go about doing this, so any help is very much appreciated. What I need is a peice of code added to the code below that means that when all the timers reach zero the page in another window is changed to a different page. If anyone has any idea of how to do this i would be mega thankful!

Cat x

<html>
<head>
<script language="javascript">
<!--
var hungerValue='10';
var moodValue='10';
var healthValue='10';
var fitnessValue='10';
var energyValue='10';

function hungerScript(){
if(hungerValue>=10){
hungerValue=10
}
if ( hungerValue == 10 || hungerValue == 0 )
document.f1.elements[0].disabled = true;
else
document.f1.elements[0].disabled = false;
countdown.innerHTML=hungerValue;
if(hungerValue<=0){
clearTimeout(Timer1)
}
else{
hungerValue--;
Timer1=setTimeout("hungerScript();", 3000);
}
}
function moodScript(){
if(moodValue>=10){
moodValue=10
}
if ( moodValue == 10 || moodValue == 0 )
document.f2.elements[0].disabled = true;
else
document.f2.elements[0].disabled = false;
countdown2.innerHTML=moodValue;
if(moodValue<=0){
clearTimeout(Timer2)
}
else{
moodValue--;
Timer2=setTimeout("moodScript();", 3000);
}
}
function healthScript(){
if(healthValue>=10){
healthValue=10
}
if ( healthValue == 10 || healthValue == 0 )
document.f3.elements[0].disabled = true;
else
document.f3.elements[0].disabled = false;
countdown3.innerHTML=healthValue;
if(healthValue<=0){
clearTimeout(Timer3)
}
else{
healthValue--;
Timer3=setTimeout("healthScript();", 3000);
}
}
function fitnessScript(){
if(fitnessValue>=10){
fitnessValue=10
}
if ( fitnessValue == 10 || fitnessValue == 0 )
document.f4.elements[0].disabled = true;
else
document.f4.elements[0].disabled = false;
countdown4.innerHTML=fitnessValue;
if(fitnessValue<=0){
clearTimeout(Timer4)
}
else{
fitnessValue--;
Timer4=setTimeout("fitnessScript();", 3000);
}
}
function energyScript(){
if(energyValue>=10){
energyValue=10
}
if ( energyValue == 10 || energyValue == 0 )
document.f5.elements[0].disabled = true;
else
document.f5.elements[0].disabled = false;
countdown5.innerHTML=energyValue;
if(energyValue<=0){
clearTimeout(Timer5)
}
else{
energyValue--;
Timer5=setTimeout("energyScript();", 3000);
}
}
function feed(){
clearTimeout(Timer1)
hungerValue+=2
hungerScript()
}
function play(){
clearTimeout(Timer2)
moodValue+=2
moodScript()
}
function medicine(){
clearTimeout(Timer3)
healthValue+=2
healthScript()
}
function exercise(){
clearTimeout(Timer4)
fitnessValue+=2
fitnessScript()
}
function bed(){
clearTimeout(Timer5)
energyValue+=2
energyScript()
}
//-->
</script>
<title>Virtual Pet - Version 1.0</title>
</head>
<body onload="hungerScript();moodScript();healthScript();fitnessScript();energyScript();" bgcolor="#0099FF" text="#ffff99" link="#00ff00" alink="#ffa500" vlink="#ff0000">
<basefont size="3">
<table width="500" valign="top" border="0">
<tr>
<td width="190" valign="left" align="left" border="0">
<table width="190" valign="top" border="1">
<td width="190" height="100" bgcolor="#FF9900" valign="left" align="left" border="1">
Hunger:</br>
Mood:</br>
Health:</br>
Fitness:</br>
Energy:</br>
</td>
<td width="40" bgcolor="#FF9900" valign="left" align="left" border="0">
<span id="countdown"></span></br>
<span id="countdown2"></span></br>
<span id="countdown3"></span></br>
<span id="countdown4"></span></br>
<span id="countdown5"></span></br>
</td>
</table>
</td>
<td width="180" valign="left" align="left" border="0">
</td>
<td width="180" valign="left" align="left" border="0">
<table width="180" valign="top" border="1">
<td width="180" height="100" bgcolor="#FF9900" valign="left" align="left" border="1">
<table width="180" valign="top" border="1">
<tr>
<td width="40" bgcolor="#FF9900" valign="left" align="left" border="1">
<form name="f1" action="feed.html" target="virtualpet">
<input type="submit" value="Feed" onclick="feed()" style="background: #ff3600; font-weight: bold">
</form>
</td>
<td width="40" bgcolor="#FF9900" valign="left" align="left" border="1">
<form name="f2" action="play.html" target="virtualpet">
<input type="submit" value="Play" onclick="play()" style="background: #ff3600; font-weight: bold">
</form>
</td>
<td width="40" bgcolor="#FF9900" valign="left" align="left" border="1">
<form name="f3" action="medicine.html" target="virtualpet">
<input type="submit" value="Medicine" onclick="medicine()" style="background: #ff3600; font-weight: bold">
</form>
</td>
</tr>
<tr>
<td width="40" bgcolor="#FF9900" valign="left" align="left" border="1">
<form name="f4" action="exercise.html" target="virtualpet">
<input type="submit" value="Exercise" onclick="exercise()" style="background: #ff3600; font-weight: bold">
</form>
</td>
<td width="40" bgcolor="#FF9900" valign="left" align="left" border="1">
<form name="f5" action="sleep.html" target="virtualpet">
<input type="submit" value="Bed" onclick="bed()" style="background: #ff3600; font-weight: bold"><br>
</form>
</td>
</tr>
</table>

</td>
</table>
</td>
</tr>
</table>
</body>
</html>

Roelf
01-12-2003, 10:01 PM
do you really mean when all the timers reach zero, or do you mean when all the values reach zero, if thats the case:

something like:

function checkfordeath () {
if ((hungerValue == 0) && (moodValue == 0) && (healthValue == 0) && (fitnessValue == 0) && (energyValue == 0)) {
window.location.href = "yourpage.htm"
}
}

do a: setInterval("checkfordeath()", 1000);

somewhere in the scriptsection on your page, the function checks every second for all values being zero.

Maybe you want to rename the function :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum