Tea Forums & Tea Industry Forum - Gavin Newsom - Debt Consolidation - Myspace Layouts - Web directory

PDA

View Full Version : I get variable NAMES and VALUES of variables mixed up


ycc
Feb 18th 2009, 5:42 pm
Hello guys,

Some time ago I got good help with the setTimeout function (by member lp1051). I kept following the pattern he adviced me to follow and it works well. However, I have some problems understanding it and this sometimes gives me trouble when I make changes to the code.

The situation occurs when I pass a function with variables to the setTimeout function.

In the following example span_up_box is the function I have written. It takes the following arguments: one string variable (szDivID) followed by four numerical variables. At least that is how I look upon the variable types, maybe my misunderstanding lies here.

However when I pass the arguments of the function span_up_box to setTimeout I MUST INCLUDE THE FIRST VARIABLE IN SINGLE QUOTES ('). I don't understand why. It is a string-variable that can take different values. (It is not a string but a variable name.)

However, the rest of the arguments are also variables, but these do NOT have to be included in single quotes.

window.setTimeout("span_up_box('" + szDivID + "', " + final_x + ", " + final_y_lift + ", " + final_width + ", " + final_height + ")", 12);


I can also put EVERY variable in single quotes and it works:

window.setTimeout("span_up_box('" + szDivID + "', '" + final_x + "', '" + final_y_lift + "', '" + final_width + "', '" + final_height + "')", 12);

(But readability is not so good, at least not to me.)


Thanks for advice.

dimitar christoff
Feb 18th 2009, 5:47 pm
the div id is string and as such neeeds to be encapsulated. szDivID will to be numeric. rest are.

this is same as:
var bar = 'foo', bar = 1; // ok
va bar = '1'; // also ok but string.

otherwise, w/o the quotes you get functionname(bar, n, n, n); and bar is not defined. you need functionname('bar', n ...

ycc
Feb 18th 2009, 7:12 pm
Thanks for reply. I am sure the reply is good and I try to understand it. However I still have difficulties with the Javascript variables. I don't remember the precise code but once I was adding numbers (I thought) but 35 + 6 did not turn up 41 but I finally understood I was adding strings and the "sum" was 356!

When I just call the function in the code I can call it using plain variables, but when I call it through setTimeout I must add single quotes around the first variable.

This is how the funciton can be called in the code:

span_up_box(box, final_x, final_y_lift, final_width, final_height);

This is what the function looks like:
function tog_box(box, final_x, final_y_lift, final_width, final_height) {
(The parameters sent to the function have the same names as the formal parameters used in the function, using setTimeout it is a recursive function.)