By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,253 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,253 IT Pros & Developers. It's quick & easy.

how to pass a parameter inside a setTimeout call

P: n/a
this works:
document.all[sResponseDivID].style.visibility = 'hidden';

this doesn't
setTimeout("document.all[sResponseDivID].style.visibility = 'hidden'",1000);

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

tks
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Lee
Dica said:

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

this doesn't
setTimeout("document.all[sResponseDivID].style.visibility = 'hidden'",1000);

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


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("document.all["+sResponseDivID+"].style.visibility = 'hidden'",1000)

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

Jul 23 '05 #2

P: n/a
"Lee" <RE**************@cox.net> wrote in message
news:ct*********@drn.newsguy.com...
Dica said:

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

this doesn't
setTimeout("document.all[sResponseDivID].style.visibility = 'hidden'",1000);
looks like sResponseDivID isn't recognized. what's the problem?
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("document.all["+sResponseDivID+"].style.visibility =

'hidden'",1000)

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

function doAnswerResponse(iResponseID, bCorrect, sFeedbackTxt, sFeedbackImg,
sSoundFileName){
var sResponseDivID = "response_" + iResponseID;
var sResponseTxtID = "responseTxt_" + iResponseID;
var sResponseImgID = "responseImg_" + iResponseID;
document.all[sResponseDivID].style.visibility = 'visible';
document.all[sResponseTxtID].innerHTML = sFeedbackTxt;
document.images[sResponseImgID].src = 'lms_modules/images/' + sFeedbackImg;
document[sSoundFileName].play();
if(bCorrect == 0){
setTimeout("document.all["+sResponseDivID+"].style.visibility =
'hidden'",1000)
}
}

You do know that document.all is out-dated and not cross-browser, right?
yes, i know it's not cross-broswser, but what about the out of date
business? what's the currently recommended approach?

Jul 23 '05 #3

P: n/a
"Dica" <ge*****@hotmail.com> wrote in message
news:os********************@rogers.com...
"Lee" <RE**************@cox.net> wrote in message
news:ct*********@drn.newsguy.com...
Dica said:
>
>this works:
>document.all[sResponseDivID].style.visibility = 'hidden';
>
>this doesn't
>setTimeout("document.all[sResponseDivID].style.visibility = 'hidden'",1000); >
>looks like sResponseDivID isn't recognized. what's the problem?


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("document.all["+sResponseDivID+"].style.visibility =

'hidden'",1000)

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

setTimeout("document.all["+sResponseDivID+"].style.visibility =
'hidden'",1000)


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("document.all['" + sResponseDivID + "'].style.visibility =
'hidden'", 1000);
</script>

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

document.all['test'].style.visibility = 'hidden';

and not:

document.all[test].style.visibility = '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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.