How can I let the program wait/sleep?

Discussion in 'Javascript' started by Alexander Erlich, Jun 1, 2005.

  1. 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
    Alexander Erlich, Jun 1, 2005
    #1
    1. Advertising

  2. Alexander Erlich

    Random Guest

    Alexander Erlich wrote:
    > 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.
    Random, Jun 1, 2005
    #2
    1. Advertising

  3. Alexander Erlich

    Random Guest

    Alexander Erlich wrote:
    > 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.
    Random, Jun 1, 2005
    #3
  4. Alexander Erlich

    Lee Guest

    Alexander Erlich said:

    >I have written a function that returns a random number in dependence of the
    >time running on the current system.


    Javascript provides a random function:
    http://www.jibbering.com/faq/#FAQ4_22
    Lee, Jun 1, 2005
    #4
  5. JRS: In article <d7ksq7$v62$00$-online.com>, dated Wed, 1 Jun
    2005 19:55:20, seen in news:comp.lang.javascript, Alexander Erlich
    <> posted :

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

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Jun 2, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gonzalo Moreno
    Replies:
    2
    Views:
    12,374
  2. Douwe
    Replies:
    3
    Views:
    8,455
    Randy Howard
    Aug 23, 2003
  3. Sam Roberts
    Replies:
    16
    Views:
    363
    Daniel Berger
    Feb 18, 2005
  4. uncutstone
    Replies:
    4
    Views:
    500
    uncutstone
    Apr 22, 2006
  5. Richard
    Replies:
    7
    Views:
    213
    Richard
    May 22, 2007
Loading...

Share This Page