how to pass a parameter inside a setTimeout call

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

    #1

    how to pass a parameter inside a setTimeout call

    this works:
    document.all[sResponseDivID].style.visibili ty = 'hidden';

    this doesn't
    setTimeout("doc ument.all[sResponseDivID].style.visibili ty = 'hidden'",1000) ;

    looks like sResponseDivID isn't recognized. what's the problem?

    tks


  • Lee

    #2
    Re: how to pass a parameter inside a setTimeout call

    Dica said:[color=blue]
    >
    >this works:
    >document.all[sResponseDivID].style.visibili ty = 'hidden';
    >
    >this doesn't
    >setTimeout("do cument.all[sResponseDivID].style.visibili ty = 'hidden'",1000) ;
    >
    >looks like sResponseDivID isn't recognized. what's the problem?[/color]

    sResponseDivID has a value at the time and in the scope that you
    call setTimeout, but that string is parsed as a Javascript expression
    at another time and in another scope. Instead of putting the name of
    the variable in the string, put the value in it:

    setTimeout("doc ument.all["+sResponseDivI D+"].style.visibili ty = 'hidden'",1000)

    You do know that document.all is out-dated and not cross-browser, right?

    Comment

    • Dica

      #3
      Re: how to pass a parameter inside a setTimeout call

      "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
      news:ct6cal026b f@drn.newsguy.c om...[color=blue]
      > Dica said:[color=green]
      > >
      > >this works:
      > >document.all[sResponseDivID].style.visibili ty = 'hidden';
      > >
      > >this doesn't
      > >setTimeout("do cument.all[sResponseDivID].style.visibili ty =[/color][/color]
      'hidden'",1000) ;[color=blue][color=green]
      > >
      > >looks like sResponseDivID isn't recognized. what's the problem?[/color]
      >
      > sResponseDivID has a value at the time and in the scope that you
      > call setTimeout, but that string is parsed as a Javascript expression
      > at another time and in another scope. Instead of putting the name of
      > the variable in the string, put the value in it:
      >
      > setTimeout("doc ument.all["+sResponseDivI D+"].style.visibili ty =[/color]
      'hidden'",1000)

      tks lee, but still doesn't work. i'm still getting "document.a ll[...].style
      is null or not an object". this is the full code:

      function doAnswerRespons e(iResponseID, bCorrect, sFeedbackTxt, sFeedbackImg,
      sSoundFileName) {
      var sResponseDivID = "response_" + iResponseID;
      var sResponseTxtID = "responseTx t_" + iResponseID;
      var sResponseImgID = "responseIm g_" + iResponseID;
      document.all[sResponseDivID].style.visibili ty = 'visible';
      document.all[sResponseTxtID].innerHTML = sFeedbackTxt;
      document.images[sResponseImgID].src = 'lms_modules/images/' + sFeedbackImg;
      document[sSoundFileName].play();
      if(bCorrect == 0){
      setTimeout("doc ument.all["+sResponseDivI D+"].style.visibili ty =
      'hidden'",1000)
      }
      }
      [color=blue]
      >
      > You do know that document.all is out-dated and not cross-browser, right?[/color]

      yes, i know it's not cross-broswser, but what about the out of date
      business? what's the currently recommended approach?[color=blue]
      >[/color]


      Comment

      • Grant Wagner

        #4
        Re: how to pass a parameter inside a setTimeout call

        "Dica" <genpub5@hotmai l.com> wrote in message
        news:os-dncjMWZAlJmvcRV n-jg@rogers.com.. .[color=blue]
        > "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
        > news:ct6cal026b f@drn.newsguy.c om...[color=green]
        >> Dica said:[color=darkred]
        >> >
        >> >this works:
        >> >document.all[sResponseDivID].style.visibili ty = 'hidden';
        >> >
        >> >this doesn't
        >> >setTimeout("do cument.all[sResponseDivID].style.visibili ty =[/color][/color]
        > 'hidden'",1000) ;[color=green][color=darkred]
        >> >
        >> >looks like sResponseDivID isn't recognized. what's the problem?[/color]
        >>
        >> sResponseDivID has a value at the time and in the scope that you
        >> call setTimeout, but that string is parsed as a Javascript expression
        >> at another time and in another scope. Instead of putting the name of
        >> the variable in the string, put the value in it:
        >>
        >> setTimeout("doc ument.all["+sResponseDivI D+"].style.visibili ty =[/color]
        > 'hidden'",1000)
        >
        > tks lee, but still doesn't work. i'm still getting
        > "document.a ll[...].style
        > is null or not an object". this is the full code:
        >
        > setTimeout("doc ument.all["+sResponseDivI D+"].style.visibili ty =
        > 'hidden'",1000)[/color]

        The problem is that you are sustituting the _value_ of sResponseDivID
        into the square brackets, and that value is not quoted.

        <div id="test">TEST </div>
        <script type="text/javascript">
        var sResponseDivID = 'test';
        setTimeout("doc ument.all['" + sResponseDivID + "'].style.visibili ty =
        'hidden'", 1000);
        </script>

        Note the single quotes immediately inside the square brackets. This
        ensures that setTimeout() is evaluating the following:

        document.all['test'].style.visibili ty = 'hidden';

        and not:

        document.all[test].style.visibili ty = 'hidden';

        Please also note that if sResponseDivID must be defined as part of the
        global object (ie - not in a function), because it _must_ be in scope
        when the setTimeout() executes the code.

        --
        Grant Wagner <gwagner@agrico reunited.com>
        comp.lang.javas cript FAQ - http://jibbering.com/faq


        Comment

        Working...