• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

setTimeout problems.

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a clock function that works well. I extended the function in order to toggle between a 12 hour clock and a 24 hour clock. An error occurs when I use the setTimeout function. The error tells me that the variable I used does not exist. Can I not pass a variable in setTimeout().
var newTime = timeType;
setTimeout('startclock(newTime)',1000);
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable newTime is probably a local variable so you can not reference it globally. You need to build it up as a string instead.

setTimeout('startclock("' + newTime + '")',1000);

Eric
 
William Rouse
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric:
Your suggestion worked, but I don�t understand why building a string to name the variable works. Could you explain this to me.
Thanks!
WBR
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this code:


The setTimeout when called is saying newTime is undefined.

The above is equivilant of doing this:



The setTimeout (I am blank here, blame it on friday before long weekend so terms may be wrong) is bascially executes in a seperate thread/instance and is not executed inside of that function that is is declared.

So by building up the string it is basically hardcoding that value into the call

looks like


It gets even better with closures and OO JavaScript.

Eric
 
William Rouse
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, that�s funny to me! Well I�m going to put on my seatbelt and wait!
Thanks Eric, it makes sense.
WBR
 
reply
    Bookmark Topic Watch Topic
  • New Topic