settimeout problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gordan

    #1

    settimeout problem

    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.b utton.value="CL OSE (auto close in "+ x +" sec)";
    x--;
    setTimeout("aut o_close(x);",10 00);
    } else self.close();
    }

    and i put onload=auto_clo se(10)

    the problem is that the first time the recursion is called (
    setTimeout("aut o_close(x);",10 00);) IE says x is undifined
    i dont understand!!

    please help, Gordan


  • Douglas Crockford

    #2
    Re: settimeout problem

    > i want to have a "close" button that user can click but that will also[color=blue]
    > "click itself after 10 sec"[/color]
    [color=blue]
    > function auto_close(x)
    > {
    > if (x > 0){ document.form.b utton.value="CL OSE (auto close in "+ x +" sec)";
    > x--;
    > setTimeout("aut o_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("aut o_close(x);",10 00);) IE says x is undifined[/color]

    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(func tion () {
    auto_close(x);
    }, 1000);

    Static binding gives that function access to x.



    Comment

    • Gordan

      #3
      Re: settimeout problem

      Thanks!

      Gordan :-)


      "Douglas Crockford" <nospam@laserli nk.net> wrote in message
      news:bkc866$bvu $1@sun-news.laserlink. net...[color=blue][color=green]
      > > i want to have a "close" button that user can click but that will also
      > > "click itself after 10 sec"[/color]
      >[color=green]
      > > function auto_close(x)
      > > {
      > > if (x > 0){ document.form.b utton.value="CL OSE (auto close in "+ x +"[/color][/color]
      sec)";[color=blue][color=green]
      > > x--;
      > > setTimeout("aut o_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("aut o_close(x);",10 00);) IE says x is undifined[/color]
      >
      > x is a local var of auto_close. When setTimeout finally evaluates the[/color]
      string, it[color=blue]
      > 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(func tion () {
      > auto_close(x);
      > }, 1000);
      >
      > Static binding gives that function access to x.
      >
      > http://www.crockford.com/javascript/inheritance.html
      >[/color]


      Comment

      Working...