setTimeout in recursive function

I've been messing with this piece of code for 3 hours now, and I can't get it to work. I'm not a javascript programmer so I can't really get anywhere. Here's the code:

<script language='JavaScript'>

arrDmgValue = getValues('<%=DmgValueStr%>');
arrDmgName = getValues('<%=DmgNameStr%>');
arrDmgSpeed = getValues('<%=DmgSpeedStr%>');
arrDmgWho = getValues('<%=DmgWhoStr%>');


//DMG = DAMAGE(ARRAY)
//NAME = WEAPON NAME(ARRAY)
//WHO = ATTACKER OR DEFENER(1 OR 0)(ARRAY)
//UNAME = DEFENDERS USERNAME
//SPD = SPEED, NOT IMPLEMENTED YET(ARRAY)
//POS = POSITION IN AN ARRAY
function showBattle(dmg,name,who,uname,spd,pos) {

//IF POSITION REACHED END OF ARRAY THEN END THE FUNCTION(ALL ARRAYS HAVE SAME NUMBER OF VALUES)
            if(pos==who.length) {
                  return;
            }
            else {
            //IF ATTACKER HITS THEN IT DISPLAY THIS MSG
                  if(who[pos]==1)
                        var newText = document.createTextNode("You hit " + uname + " for " + dmg[pos] + " points of damage with your " + name[pos]);
            //OTHERWISE THIS ONE
                  else
                        var newText = document.createTextNode(uname + " hits you for " + dmg[pos] + " points of damage with his " + name[pos]);
            //DISPLAY IT ON THE SCREEN
                  var para = document.getElementsByTagName("P").item(0);
                  para.firstChild.data = "BATTLE BEGINS:";
                  para.appendChild(newText);
            //MOVE ONE POSITION UP IN ALL OF THE ARRAYS
                  pos = pos*1 + 1;
            //WAIT 2 SECONDS THEN REPEAT
                  window.setTimeout("showBattle('" + dmg + "','" + name + "','" + who + "','" + uname + "','" + spd + "','" + pos + "')", 2000);
            }
}

//FIRST FUNCTION RUN, TAKES WHOLE ARRAYS AND SENDS THEM TO THE FUNCTION
showBattle(arrDmgValue,arrDmgName,arrDmgWho,'<%=defName%>', arrDmgSpeed, 0)
</script>

Arrays are sorted like this for example:

who(1,0,1,1,)
damage(15,26,17,17)
name("test1","test2","test","test")

So, first run is Attacker for 15 dmg with weapon test1, and so on...

The thing is I only get the first run, on the next run all array values say "undefined"! So:

BATTLE BEGINS:
1st funct. run: You hit Enemy for 22 points of damage with your 20mm Cannon
2nd funct. run: Enemy hits you for undefined points of damage with his undefined
and so on...

I know that array index(pos variable) is increasing fine, and I know that I'm getting the arrays but somehow writing dmg[pos] retrieves undefined, while pos itself returns(For example) 1(on the second run), and dmg itself(without index specified) return 15,26,17,17.

Please help


Thanks in advance :)
BearishSunAsked:
Who is Participating?
 
ZvonkoConnect With a Mentor Systems architectCommented:
Perhaps like this:

<html>
<head>
<script language='JavaScript'>
function getValues(str) {
var getValuess = str.split(";");
return(getValuess);
}


</Script>
</head>
<body>

<p id="p1">Ada</p>

</body>
</html>
<script language='JavaScript'>

arrDmgValue = getValues('16;24;24;18;21;14;26;18;25;29;21;28;17;34;14;22;18;17;15;25;20;28;21;26;28;27;30;14;23;26;27;35;16;14;24;29');
arrDmgName = getValues('20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon');
arrDmgSpeed = getValues('10;10;10;10;10;10;10;10;10;20;20;20;20;20;20;20;20;20;40;40;40;40;40;40;40;40;40;80;80;80;80;80;80;80;80;80');
arrDmgWho = getValues('1;1;1;1;1;0;0;0;0;1;1;1;1;1;0;0;0;0;1;1;1;1;1;0;0;0;0;1;1;1;1;1;0;0;0;0');

function arrToString(arr) {

for(i=0;i < arr.length;i++) {
if(arrToString=="")
arrToString = arr[i];
else
arrToString = arrToString + ";" + arr[i];
return(arrToString);
}


}

//DMG = DAMAGE
//NAME = WEAPON NAME
//WHO = ATTACKER OR DEFENER(1 OR 0)
//UNAME = DEFENDERS USERNAME
//SPD = SPEED, NO IMPLEMENTED YET
//POS = POSITION IN AN ARRAY
function showBattle(dmg,name,who,uname,spd,pos) {
//IF POSITION REACHED END OF ARRAY END FUNCTION(ALL ARRAYS HAVE SAME NUMBER OF VALUES)
         if(pos==who.length) {
              return;
         }
         else {
         

         //IF ATTACKER HITS DISPLAY THIS MSG
              if(who[pos]==1)
                   var newText = document.createTextNode("You hit " + uname + " for " + window[dmg][pos] + " points of damage with your " + window[name][pos]);
         //OTHERWISE THIS ONE
              else
                   var newText = document.createTextNode(uname + " hits you for " + window[dmg][pos] + " points of damage with his " + window[name][pos]);
         //DISPLAY IT ON THE SCREEN
              var para = document.getElementsByTagName("P").item(0);
              para.firstChild.data = "BATTLE BEGINS:";
              br = document.createElement("br");
              para.appendChild(br);
              para.appendChild(newText);
         //MOVE ONE POSITION UP IN ALL OF THE ARRAYS
              pos = pos*1 + 1;
         //WAIT 2 SECONDS THEN REPEAT
         
         
              window.setTimeout("showBattle('" + dmg + "','" + name + "','" + who + "','" + uname + "','" + spd + "','" + pos + "')", 2000);
         }
}
//FIRST FUNCTION RUN, TAKES WHOLE ARRAYS AND SENDS THEM TO THE FUNCTION
showBattle('arrDmgValue','arrDmgName','arrDmgWho','allah', 'arrDmgSpeed', 0)
</script>

0
 
BearishSunAuthor Commented:
No one responding, increased points to 300
0
 
lil_puffballCommented:
Would you mind posting the generated html code? It's easier to work with code you can just copy & paste into notepad.
It's always slow on sunday afternoons, that's probably why no one has replied. ;)
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
BearishSunAuthor Commented:
hehe, here's the HTML:



<html>
<head>
<script language='JavaScript'>
function getValues(str) {
var getValuess = str.split(";");
return(getValuess);
}


</Script>
</head>
<body>

<p id="p1">Ada</p>

</body>
</html>
<script language='JavaScript'>

arrDmgValue = getValues('16;24;24;18;21;14;26;18;25;29;21;28;17;34;14;22;18;17;15;25;20;28;21;26;28;27;30;14;23;26;27;35;16;14;24;29');
arrDmgName = getValues('20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon');
arrDmgSpeed = getValues('10;10;10;10;10;10;10;10;10;20;20;20;20;20;20;20;20;20;40;40;40;40;40;40;40;40;40;80;80;80;80;80;80;80;80;80');
arrDmgWho = getValues('1;1;1;1;1;0;0;0;0;1;1;1;1;1;0;0;0;0;1;1;1;1;1;0;0;0;0;1;1;1;1;1;0;0;0;0');

function arrToString(arr) {

for(i=0;i < arr.length;i++) {
if(arrToString=="")
arrToString = arr[i];
else
arrToString = arrToString + ";" + arr[i];
return(arrToString);
}


}

//DMG = DAMAGE
//NAME = WEAPON NAME
//WHO = ATTACKER OR DEFENER(1 OR 0)
//UNAME = DEFENDERS USERNAME
//SPD = SPEED, NO IMPLEMENTED YET
//POS = POSITION IN AN ARRAY
function showBattle(dmg,name,who,uname,spd,pos) {
//IF POSITION REACHED END OF ARRAY END FUNCTION(ALL ARRAYS HAVE SAME NUMBER OF VALUES)
            if(pos==who.length) {
                  return;
            }
            else {
            

            //IF ATTACKER HITS DISPLAY THIS MSG
                  if(who[pos]==1)
                        var newText = document.createTextNode("You hit " + uname + " for " + dmg[pos] + " points of damage with your " + name[pos]);
            //OTHERWISE THIS ONE
                  else
                        var newText = document.createTextNode(uname + " hits you for " + dmg[pos] + " points of damage with his " + name[pos]);
            //DISPLAY IT ON THE SCREEN
                  var para = document.getElementsByTagName("P").item(0);
                  para.firstChild.data = "BATTLE BEGINS:";
                  para.appendChild(newText);
            //MOVE ONE POSITION UP IN ALL OF THE ARRAYS
                  pos = pos*1 + 1;
            //WAIT 2 SECONDS THEN REPEAT
            
            
                  window.setTimeout("showBattle('" + dmg + "','" + name + "','" + who + "','" + uname + "','" + spd + "','" + pos + "')", 2000);
            }
}
//FIRST FUNCTION RUN, TAKES WHOLE ARRAYS AND SENDS THEM TO THE FUNCTION
showBattle(arrDmgValue,arrDmgName,arrDmgWho,'allah', arrDmgSpeed, 0)
</script>
0
 
ZvonkoSystems architectCommented:
In your setTimout zou try to pass an array in a String. That is not possible.
0
 
BearishSunAuthor Commented:
could you explain little more please? as I said, I haven't worked with javascript too much
0
 
BearishSunAuthor Commented:
HAHA THANK YOU THANK YOU THANK YOU!!! SO DAMN MUCH LOL I'VE BEEN STRUGGLING WITH THIS WHOLE DAY NOW :)))) THANKS BRO
0
 
ZvonkoSystems architectCommented:
Here is the next stage of abstraction.
Now you pass not four array, but one Object with several properties as arrays:

<html>
<head>
<script language='JavaScript'>
function getValues(str) {
var getValuess = str.split(";");
return(getValuess);
}


</Script>
</head>
<body>

<p id="p1">Ada</p>

</body>
</html>
<script language='JavaScript'>
var battle = new Object();
battle.dmgValue = getValues('16;24;24;18;21;14;26;18;25;29;21;28;17;34;14;22;18;17;15;25;20;28;21;26;28;27;30;14;23;26;27;35;16;14;24;29');
battle.dmgName = getValues('20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;25mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon;20mm Cannon');
battle.dmgSpeed = getValues('10;10;10;10;10;10;10;10;10;20;20;20;20;20;20;20;20;20;40;40;40;40;40;40;40;40;40;80;80;80;80;80;80;80;80;80');
battle.dmgWho = getValues('1;1;1;1;1;0;0;0;0;1;1;1;1;1;0;0;0;0;1;1;1;1;1;0;0;0;0;1;1;1;1;1;0;0;0;0');

function arrToString(arr) {

for(i=0;i < arr.length;i++) {
if(arrToString=="")
arrToString = arr[i];
else
arrToString = arrToString + ";" + arr[i];
return(arrToString);
}


}

//DMG = DAMAGE
//NAME = WEAPON NAME
//WHO = ATTACKER OR DEFENER(1 OR 0)
//UNAME = DEFENDERS USERNAME
//SPD = SPEED, NO IMPLEMENTED YET
//POS = POSITION IN AN ARRAY
function showBattle(battleName, uname, pos) {
  var theBattle = window[battleName];
//IF POSITION REACHED END OF ARRAY END FUNCTION(ALL ARRAYS HAVE SAME NUMBER OF VALUES)
         if(pos==theBattle.dmgWho.length) {
              return;
         }
         else {
         

         //IF ATTACKER HITS DISPLAY THIS MSG
              if(theBattle.dmgWho[pos]==1)
                   var newText = document.createTextNode("You hit " + uname + " for " + theBattle.dmgValue[pos] + " points of damage with your " + theBattle.dmgName[pos]);
         //OTHERWISE THIS ONE
              else
                   var newText = document.createTextNode(uname + " hits you for " + theBattle.dmgValue[pos] + " points of damage with his " + theBattle.dmgName[pos]);
         //DISPLAY IT ON THE SCREEN
              var para = document.getElementsByTagName("P").item(0);
              para.firstChild.data = "BATTLE BEGINS:";
              br = document.createElement("br");
              para.appendChild(br);
              para.appendChild(newText);
         //MOVE ONE POSITION UP IN ALL OF THE ARRAYS
              pos = pos*1 + 1;
         //WAIT 2 SECONDS THEN REPEAT
         
         
              window.setTimeout("showBattle('"+battleName+"' ,'" + uname + "', '" + pos + "')", 2000);
         }
}
//FIRST FUNCTION RUN, TAKES WHOLE ARRAYS AND SENDS THEM TO THE FUNCTION
showBattle('battle', 'allah', 0)
</script>

0
 
ZvonkoSystems architectCommented:
Thanks for the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.