Connecting Tech Pros Worldwide Help | Site Map

Trouble using setTimeout/setInterval

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 02:44 PM
Weston C
Guest
 
Posts: n/a
Default Trouble using setTimeout/setInterval

In the course of trying to build a simple clock, I've run into a problem
using the setInterval (and setTimeout) function.

http://weston.canncentral.org/misc/tkeep/tkeep.html
http://weston.canncentral.org/misc/tkeep/tkeep.jss

function fieldToClock(fieldId)
{
var field = document.getElementById(fieldId);
alert("Starting a clock in text field " + fieldId + "(" + field
+ ")");
codeSnippet = "clockUpdate(" + fieldId + ")";
setInterval(clockUpdate(fieldId),1000);
//setInterval(codeSnippet,1000);
//setInterval("eval(\"" + codeSnippet + "\")",1000);
}

function clockUpdate(fieldId)
{
field = document.getElementById(fieldId);
field.value = date2timestr(new Date());
}

The problem line is the setInterval line, and it seems to have something
to do with the fact I want to pass the function clockUpdate an argument
(I don't want to specify a single text element to be associated with the
display of the clock. Ideally, I'd like to be able to call the function
"fieldToClock" with the id of any text field in the document and turn it
into a clock).

The two commented out lines are other approaches I've applied. The first
approach yields the error:

Error: useless setInterval call (missing quotes around argument?)
Source File: http://weston.canncentral.org/misc/tkeep/tkeep.jss
Line: 6

The second/third approach yeild:

Error: clock_STF is not defined
Source File: http://weston.canncentral.org/misc/tkeep/tkeep.jss
Line: 7

Except this error is repeated every 1000 seconds. :)

Any ideas?

Thanks,

Weston





*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

  #2  
Old July 20th, 2005, 02:44 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: Trouble using setTimeout/setInterval

Weston C <west8on[at]cann8central.RemoveEights.org> writes:
[color=blue]
> In the course of trying to build a simple clock, I've run into a problem
> using the setInterval (and setTimeout) function.[/color]
[color=blue]
> var field = document.getElementById(fieldId);[/color]
[color=blue]
> codeSnippet = "clockUpdate(" + fieldId + ")";[/color]

So, fieldId is a string. Say it is the string "foobar". Then your
codeSnippet becomes the string
"clockUpdate(foobar)"
Here, foobar is not a string, but a variable name, because the quotes
are missing. Try:
codeSnippet = "clockUpdate(\""+fieldId+"\");";
[color=blue]
> setInterval(clockUpdate(fieldId),1000);[/color]

Here, you call the clockUpdate function right now, and try evaluating
the result of that in one second. Try:
setInterval(function(){clockUpdate(fieldId);},1000 );
or use the above codeSnippet with:[color=blue]
> //setInterval(codeSnippet,1000);[/color]
[color=blue]
> //setInterval("eval(\"" + codeSnippet + "\")",1000);[/color]

Don't use eval. Don't ever use eval (the exceptions are so rare that
you'll probably never hit them).


[color=blue]
> The two commented out lines are other approaches I've applied. The first
> approach yields the error:
>
> Error: useless setInterval call (missing quotes around argument?)
> Source File: http://weston.canncentral.org/misc/tkeep/tkeep.jss
> Line: 6[/color]

Yes, the return value of clockUpdate is undefined. It is useless to
delay "undefined" for one second.
[color=blue]
> The second/third approach yeild:
>
> Error: clock_STF is not defined
> Source File: http://weston.canncentral.org/misc/tkeep/tkeep.jss
> Line: 7[/color]

Ah, the content of your string is "clock_STF". As I said above, it
is now seen without its quotes, as a variable, and there is no
variable defined by that name.

/l
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.