Solved

setTimeout in recursive function

Posted on 2004-05-02
9
969 Views
Last Modified: 2008-03-17
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 :)
0
Comment
Question by:BearishSun
  • 4
  • 4
9 Comments
 

Author Comment

by:BearishSun
ID: 10972765
No one responding, increased points to 300
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10973014
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
 

Author Comment

by:BearishSun
ID: 10973151
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 10973188
In your setTimout zou try to pass an array in a String. That is not possible.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:BearishSun
ID: 10973243
could you explain little more please? as I said, I haven't worked with javascript too much
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 300 total points
ID: 10973249
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
 

Author Comment

by:BearishSun
ID: 10973267
HAHA THANK YOU THANK YOU THANK YOU!!! SO DAMN MUCH LOL I'VE BEEN STRUGGLING WITH THIS WHOLE DAY NOW :)))) THANKS BRO
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10973302
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 10973303
Thanks for the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now