...

View Full Version : setTimeout doesn't seem to be working...



KevinJohnson
06-21-2010, 07:37 PM
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.



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

hdewantara
06-23-2010, 08:50 AM
Hello KevinJohnson,
It's quite hard to see the logic of your script,

but anyway,
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
for (var zCounter = 1; zCounter <= RngNumbers; zCounter++){}, since the possible RngNumbers values are only 0 or 1.

Dormilich
06-23-2010, 09:05 AM
Hello KevinJohnson,
It's quite hard to see the logic of your script,

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

or even easier:
setTimeout(CheckRNG, 1000);

KevinJohnson
06-23-2010, 10:45 AM
or even easier:
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

Philip M
06-23-2010, 11:04 AM
i think i found out my problem. I should be using SetInterval() instead

as i need this function to be activated constantly

setInterval();



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum