I get variable NAMES and VALUES of variables mixed up

Discussion in 'JavaScript' started by ycc, Feb 18, 2009.

  1. #1
    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);
    Code (markup):

    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);
    Code (markup):
    (But readability is not so good, at least not to me.)


    Thanks for advice.
     
    ycc, Feb 18, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    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 ...
     
    dimitar christoff, Feb 18, 2009 IP
  3. ycc

    ycc Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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);
    Code (markup):
    This is what the function looks like:
    function tog_box(box, final_x, final_y_lift, final_width, final_height) {
    Code (markup):
    (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.)
     
    ycc, Feb 18, 2009 IP