settimeout problem

Discussion in 'Javascript' started by Gordan, Sep 18, 2003.

  1. Gordan

    Gordan Guest

    hi

    i want to have a "close" button that user can click but that will also
    "click itself after 10 sec"
    so heres what i did

    function auto_close(x)
    {
    if(x>0){ document.form.button.value="CLOSE (auto close in "+ x +" sec)";
    x--;
    setTimeout("auto_close(x);",1000);
    } else self.close();
    }

    and i put onload=auto_close(10)

    the problem is that the first time the recursion is called (
    setTimeout("auto_close(x);",1000);) IE says x is undifined
    i dont understand!!

    please help, Gordan
     
    Gordan, Sep 18, 2003
    #1
    1. Advertising

  2. > i want to have a "close" button that user can click but that will also
    > "click itself after 10 sec"


    > function auto_close(x)
    > {
    > if (x > 0){ document.form.button.value="CLOSE (auto close in "+ x +" sec)";
    > x--;
    > setTimeout("auto_close(x);", 1000);
    > } else self.close();
    > }
    >
    > and i put onload = auto_close(10);
    >
    > the problem is that the first time the recursion is called (
    > setTimeout("auto_close(x);",1000);) IE says x is undifined


    x is a local var of auto_close. When setTimeout finally evaluates the string, it
    is not inside of auto_close, so the value of x is not available.

    You can get around this by passing a function instead.

    setTimeout(function () {
    auto_close(x);
    }, 1000);

    Static binding gives that function access to x.

    http://www.crockford.com/javascript/inheritance.html
     
    Douglas Crockford, Sep 18, 2003
    #2
    1. Advertising

  3. Gordan

    Gordan Guest

    Thanks!

    Gordan :)


    "Douglas Crockford" <> wrote in message
    news:bkc866$bvu$...
    > > i want to have a "close" button that user can click but that will also
    > > "click itself after 10 sec"

    >
    > > function auto_close(x)
    > > {
    > > if (x > 0){ document.form.button.value="CLOSE (auto close in "+ x +"

    sec)";
    > > x--;
    > > setTimeout("auto_close(x);", 1000);
    > > } else self.close();
    > > }
    > >
    > > and i put onload = auto_close(10);
    > >
    > > the problem is that the first time the recursion is called (
    > > setTimeout("auto_close(x);",1000);) IE says x is undifined

    >
    > x is a local var of auto_close. When setTimeout finally evaluates the

    string, it
    > is not inside of auto_close, so the value of x is not available.
    >
    > You can get around this by passing a function instead.
    >
    > setTimeout(function () {
    > auto_close(x);
    > }, 1000);
    >
    > Static binding gives that function access to x.
    >
    > http://www.crockford.com/javascript/inheritance.html
    >
     
    Gordan, Sep 18, 2003
    #3
    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. Test
    Replies:
    0
    Views:
    90
  2. nobody

    setTimeout() problem?

    nobody, Dec 10, 2003, in forum: Javascript
    Replies:
    3
    Views:
    73
    lallous
    Dec 10, 2003
  3. BKDotCom

    setTimeout event problem??

    BKDotCom, Dec 10, 2004, in forum: Javascript
    Replies:
    2
    Views:
    72
    BKDotCom
    Dec 12, 2004
  4. iam
    Replies:
    0
    Views:
    104
  5. Replies:
    3
    Views:
    80
Loading...

Share This Page