Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem using settimeout in for loop

    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;
    }

    }

    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,983
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    This should move you forward (but there may be another more sophisticated way of retaining the passed value N):-

    Code:
    <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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •