...

View Full Version : problem using settimeout in for loop



kenjim
11-26-2008, 05:47 PM
Hi, I'm trying to learn how to use the setTimeout feature in a for loop. Here is the code that I am working with, and the settimeout feature is in bold. Basically, I want the function trials(N) to perform the function evaluateHand N times (where N is an input). A graphic is then displayed. I want the graphic to be displayed for a certain amount of time before the for loop runs again.

For example, if I input N to 5, I want it to run evaluateHand function, then delay for one second, then run the evaluateHand function again.

Thanks,
kenjim

function trials(N) {

for (i = 1; i<=N; i++) {
pip = 0;
evaluateHand();
setTimeout("trials(N)",1000);
}
}


function evaluateHand()
{
if (pip == 0) {
pip = dealTwoCards();

if (rank1 - rank2 == 0){
document.getElementById("results").innerHTML = "<h1> You have a pair! Raise it up! </h1>"; R++; T++;
pip = 0;

}
else if (4*rank2 - dealt2 == 4*rank1 - dealt1 ) {
document.getElementById("results").innerHTML = "<h1> Your cards are suited! Raise it up! </h1>";R++; T++;
pip = 0;

}
else if ( pip >= 82){
document.getElementById("results").innerHTML = "<h1>You have two high cards! Raise! </h1>"; R++; T++;
pip = 0;

}
else if (pip <= 81 ) {
document.getElementById("results").innerHTML = "<h1>Fold! Push button for new game.</h1>"; F++; T++;
pip = 0;
}

}

}

Philip M
11-26-2008, 07:17 PM
This should move you forward (but there may be another more sophisticated way of retaining the passed value N):-


<input type = "hidden" id = "passVal">

<script type = "text/javascript">
var count = 0;
function trials(N) {
if (count == 0) {
document.getElementById("passVal").value = N;
}
else {
N = parseInt(document.getElementById("passVal").value);
}
count ++;
evaluateHand();
if (count < N) {
tim = window.setTimeout("trials()",1500);
}
}

function evaluateHand() {
alert ("Hand Evaluated");
}

trials(5);

</script>


Mt. Kilimanjaro, the breathtaking backdrop for the Serena Lodge. Swim in the lovely pool while you drink it all in. - Holiday brochure



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum