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 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2009
    Location
    China
    Posts
    133
    Thanks
    1
    Thanked 1 Time in 1 Post

    setTimeout doesn't seem to be working...

    This one has me stumped, it seems that no matter what i do the indicator needle in this web app just won't move.
    The odd thing is, i'm able to verify that the function for the setTimeout is being called, but when i try to write some data to the TextArea, or even do a simple alert, ...that data is not written, nor is an alertbox activated.

    ...just for those wondering, this is a JavaScript implementation of the Micro Pk test (bell curve - obviously) from the RetroPk experiments website.

    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    var Status = "Start";
    //calculate the Z-score
    var Direction = "Left";
    var Sound = 0;
    var Zero = 0;                                             //Reset the 1s and 0s  Sum variables
    var One = 0;
    var RngNumbers = 10;                                     //Number of Random Numbers to generate
    var zScore;
    var IndicatorPos;
    var Stop = 0;
    
    function Start()
    {
    alert("starting");
    while (Stop != 0)
      {
      IndicatorPos = parseInt(document.getElementById("Indicator").style.left);
      var tick = setTimeout(CheckRNG(), 1000);
      }
    }
    
    
    function CheckRNG()
    {
    document.getElementById("debug").value = "starting loop" + "\n";
    
    for (var zCounter = 1; zCounter <= RngNumbers; zCounter++) //Generate a series of random numbers and Add the number of 1s and 0s
        {
        var RngNumber = Math.round(Math.random() * 1);
    
        if (RngNumber == 1)                                   //Count the number of 0s
           {
    	    Zero = Zero + 1;
    	   }
    	else
    	   {
            One = One + 1;                                    //Count the number of 1s
    	   }
        }
    var OldScore = zScore;
    
    var Mean = RngNumbers * (1 / 2);                           //Calculate the statistical mean of the Random number sum
    var StandardDeviation = Math.sqrt(Mean / 2);               //Calculate the standard deviation of the RNG stream
    alert("hi");
    document.getElementById("debug").value = StandardDeviation;
    
    if (Direction == "Left")                                   //If the direction for Hits is "Left" then calculate z-score for 1's
       {
        zScore = (One - Mean) / StandardDeviation;
       }
    
    if (Direction == "Right")                                  //If the direction for Hits is "Right" then calculate z-score for 0's
       {
        zScore = (Zero - Mean) / StandardDeviation;
       }
    
    //Swap the previous and current z-score variables
    if (zScore > OldScore)
       {
       document.getElementById("Indicator").style.left = IndicatorPos + 1 + "px";
       if (Sound == 1)
          {
           //playwave SoundFile$, async                       //If the sound is turned on, and there is forward progress
    	  }                                                   //play the default indication sound
       } 
    
    if (zScore < OldScore)
       {
        document.getElementById("Indicator").style.left = IndicatorPos - 1 + "px";
       if (Sound == 1)
          {
           //playwave SoundFile$, async                       //If the sound is turned on, and there is forward progress
    	  }                                                   //play the default indication sound
       }
    }
    </script>
    
    </head>
    
    <body>
    <div style="position: absolute; top: 0px; left: 0px">
    <img id="BGimage" src="BellCurve.png" style="position: relative; top: 0px; left: 0px;">
    <div id="Indicator" style="background: red; position: absolute; top: 0px; left: 393px; width: 1px; height: 302px;"></div>
    </div>
    <div style="position: relative;">
    <input type="button" value="Start" onClick="Start();">
    <input type="button" value="Stop" onClick="javascript:Stop++;">
    </div>
    <textarea id="debug" cols=10 rows=10 style="position: relative;"></textarea>
    </body>
    </html>

  • #2
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    349
    Thanks
    9
    Thanked 51 Times in 50 Posts
    Hello KevinJohnson,
    It's quite hard to see the logic of your script,

    but anyway,
    Code:
    setTimeout("CheckRNG()", 1000)
    shall start your script.

    You might also want to:
    1) Put that tick var into global. Anytime you want to stop the script, this tick var would be accessible.
    2) Review again your infinite-loop inside
    Code:
    for (var zCounter = 1; zCounter <= RngNumbers; zCounter++){}
    , since the possible RngNumbers values are only 0 or 1.
    Last edited by hdewantara; 06-23-2010 at 08:53 AM.

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by hdewantara View Post
    Hello KevinJohnson,
    It's quite hard to see the logic of your script,

    but anyway,
    Code:
    setTimeout("CheckRNG()", 1000)
    shall start your script.
    or even easier:
    Code:
    setTimeout(CheckRNG, 1000);
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Regular Coder
    Join Date
    May 2009
    Location
    China
    Posts
    133
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Dormilich View Post
    or even easier:
    Code:
    setTimeout(CheckRNG, 1000);
    i think i found out my problem. I should be using SetInterval() instead

    as i need this function to be activated constantly

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by KevinJohnson View Post
    i think i found out my problem. I should be using SetInterval() instead

    as i need this function to be activated constantly
    setInterval();


  •  

    Posting Permissions

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