how to write something like setTimeout

Discussion in 'Javascript' started by Antony Sequeira, May 20, 2004.

  1. Hi
    While looking at some code I realized that the built in
    setTimeout
    function takes a string that is later
    evaluated in the original caller's context.
    How does one achieve something similar in user defined functions.

    -Antony
    Antony Sequeira, May 20, 2004
    #1
    1. Advertising

  2. Antony Sequeira

    Lee Guest

    Antony Sequeira said:
    >
    >Hi
    >While looking at some code I realized that the built in
    >setTimeout
    >function takes a string that is later
    >evaluated in the original caller's context.
    >How does one achieve something similar in user defined functions.


    You use setTimeout.
    In other words, your question isn't clear.
    Why won't setTimeout do what you want?
    Lee, May 20, 2004
    #2
    1. Advertising

  3. Antony Sequeira wrote:

    > While looking at some code I realized that the built in
    > setTimeout
    > function takes a string that is later
    > evaluated in the original caller's context.


    This original context is always the global object (window). However, you
    can perfectly use a function reference or a function expression instead
    of a string as the first argument for setTimeout - this isn't supported
    by old browsers though, which would then call the toString() method for
    the function while evaluating the setTimeout first argument - test
    accordingly.

    function foo(){
    var bar="hello, world!";
    setTimeout(
    function(){
    alert(bar);
    },
    1000
    );
    }

    Using a function expression is much more powerful than using a string
    argument, since you can control the scope chain and add a specific scope
    object, just for this function (either using an outer function or a
    "with" statement).

    ---
    setTimeout(
    (function(){
    var foo=0, bar=0;
    return function(){
    window.status="foo:"+foo+", bar:"+bar;
    if(foo++<1000 && bar-->-1000)
    setTimeout(arguments.callee, 50);
    }
    })(),
    50
    );
    ---


    ---
    with({foo:0,bar:0}){
    setTimeout(
    function(){
    window.status="foo:"+foo+", bar:"+bar;
    if(foo++<1000 && bar-->-1000)
    setTimeout(arguments.callee, 50);
    },
    50
    );
    }
    ---


    HTH
    Yep.
    Yann-Erwan Perio, May 20, 2004
    #3
  4. Yann-Erwan Perio wrote:

    > Antony Sequeira wrote:
    >
    >> While looking at some code I realized that the built in
    >> setTimeout
    >> function takes a string that is later
    >> evaluated in the original caller's context.

    >
    >
    > This original context is always the global object (window).

    Ok. Thanks for correcting my mis-conception and for the example code.
    -Antony
    Antony Sequeira, May 20, 2004
    #4
    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. Peter Hansen
    Replies:
    1
    Views:
    369
    Joona I Palaste
    Aug 24, 2003
  2. Tony Johansson
    Replies:
    2
    Views:
    718
    Patrice
    Jan 9, 2010
  3. Replies:
    3
    Views:
    85
    Gregory Brown
    Nov 13, 2005
  4. Knud
    Replies:
    1
    Views:
    105
    Randy Webb
    Aug 30, 2004
  5. dmitrey
    Replies:
    5
    Views:
    398
    Jussi Piitulainen
    Apr 20, 2012
Loading...

Share This Page