473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to pass a parameter inside a setTimeout call

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
3 4470
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Radde | last post by:
HI all, Whats the difference b/w pass by ref and pass by pointer in C++ when ur passing objects as args.. Cheers..
6
by: M B HONG 20 | last post by:
Hi all - I'm trying to emulate a sleep function in javascript. I know of this method for example: window.setTimeout("function1();", 500); However, I was wondering if there was a way to...
7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
4
by: deko | last post by:
This is a basic program flow question. I'm trying to refractor an AC2000 app and split sections of code into separate modules. But there are a number of collections I create in one big module -...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
3
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an...
13
by: pauldepstein | last post by:
My texts give plenty of examples where passing by reference is necessary. For instance, the famous swap example. However, I've never seen an example where passing a variable by value is...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.