kenjim
11-26-2008, 04: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;
}
}
}
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;
}
}
}