Setting variable names from strings

Results 1 to 2 of 2

Thread: Setting variable names from strings

  1. #1
    Join Date
    Dec 1969

    Default Setting variable names from strings

    Is it possible to set variable names from strings? I want certain variables to be created with a specific name that depends on the page element calling it. Is this even possible??<BR><BR>EX: element with id = tester calls the function then i want something like testerTimer = setTimeOut("blah", 1)

  2. #2
    Join Date
    Dec 1969

    Default Use it with care, but sure...

    eval<BR><BR>Look it up in the JavaScript docs.<BR><BR>function setNamedTimeout( name, howLong )<BR>{<BR>&nbsp; &nbsp; tname = "timeout_" + name<BR>&nbsp; &nbsp; eval( tname + " = setTimeout(&#039;blah()&#039;," + howLong + ")" );<BR>}<BR><BR>Ugly, but I think it works. (If not, it&#039;s because of a typo on my part, more than likely. The concept is correct.)<BR><BR>But have you considered using an associative array, instead?<BR><BR>Something like:<BR><BR>var timers = new Array( );<BR><BR>function setNamedTimeout( name, howLong )<BR>{<BR>&nbsp; &nbsp; timers[name] = setTimeout(&#039;blah()&#039;, howLong );<BR>}<BR><BR>And then you can access any of them via:<BR><BR>clearTimeout( timers["foobar"] );<BR><BR>or similar.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts