How can I let the program wait/sleep?

  • Thread starter Alexander Erlich
  • Start date
A

Alexander Erlich

Hello,

I have written a function that returns a random number in dependence of the
time running on the current system. The problem is that initializing two
variables with this function _one after another_, returns always the same
result:

function rndnumber () {...}

number1 = rndnumber();
number2 = rndnumber(); // number1=number2 !

I conclude that the initialization process is actually happening in the same
millisecond, which is a problem. Therefore, I would like to make the program
sleep (or call it wait ;-) for a while (e.g. some 10ms). How can I do that?

Thx a lot in advance.

Regards
Alexander
 
R

Random

Alexander said:
Hello,

I have written a function that returns a random number in dependence of the
time running on the current system. The problem is that initializing two
variables with this function _one after another_, returns always the same
result:

function rndnumber () {...}

number1 = rndnumber();
number2 = rndnumber(); // number1=number2 !

I conclude that the initialization process is actually happening in the same
millisecond, which is a problem. Therefore, I would like to make the program
sleep (or call it wait ;-) for a while (e.g. some 10ms). How can I do that?

Thx a lot in advance.

Regards
Alexander


You can't make JavaScript sleep or wait, but you can delay execution of
a particular block of code using

intPID = setTimeout( strCode, intMilliseconds )

Beware that any code that follows the setTimeout call will still be
executed -- it is a non-blocking function call. It will likely be
executed to completion before the strCode code is executed.

It will look vaguely like another thread is running simultaneously--
this is not exactly the case-- but thinking of it in that manner can be
helpful sometimes and harmful others.
 
R

Random

Alexander said:
Hello,

I have written a function that returns a random number in dependence of the
time running on the current system. The problem is that initializing two
variables with this function _one after another_, returns always the same
result:

function rndnumber () {...}

number1 = rndnumber();
number2 = rndnumber(); // number1=number2 !

I conclude that the initialization process is actually happening in the same
millisecond, which is a problem. Therefore, I would like to make the program
sleep (or call it wait ;-) for a while (e.g. some 10ms). How can I do that?

Thx a lot in advance.

Regards
Alexander

By the way, if setTimeout isn't the solution, give us the code for
rndnumber and we'll see if we can't find the problem.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 1 Jun
2005 19:55:20, seen in Alexander Erlich
I have written a function that returns a random number in dependence of the
time running on the current system. The problem is that initializing two
variables with this function _one after another_, returns always the same
result:

function rndnumber () {...}

number1 = rndnumber();
number2 = rndnumber(); // number1=number2 !

I conclude that the initialization process is actually happening in the same
millisecond, which is a problem.

(a) While the unit of new Date() is a millisecond, the update interval
can be as much as 55 milliseconds.

(b) FAQ 4.22.

(c) <URL:http://www.merlyn.demon.co.uk/js-randm.htm>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top