473,769 Members | 6,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(fi eldId)
{
var field = document.getEle mentById(fieldI d);
alert("Starting a clock in text field " + fieldId + "(" + field
+ ")");
codeSnippet = "clockUpdat e(" + fieldId + ")";
setInterval(clo ckUpdate(fieldI d),1000);
//setInterval(cod eSnippet,1000);
//setInterval("ev al(\"" + codeSnippet + "\")",1000) ;
}

function clockUpdate(fie ldId)
{
field = document.getEle mentById(fieldI d);
field.value = date2timestr(ne w 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
"fieldToClo ck" 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!
Jul 20 '05 #1
1 16109
Weston C <west8on[at]cann8central.Re moveEights.org> writes:
In the course of trying to build a simple clock, I've run into a problem
using the setInterval (and setTimeout) function. var field = document.getEle mentById(fieldI d); codeSnippet = "clockUpdat e(" + fieldId + ")";
So, fieldId is a string. Say it is the string "foobar". Then your
codeSnippet becomes the string
"clockUpdate(fo obar)"
Here, foobar is not a string, but a variable name, because the quotes
are missing. Try:
codeSnippet = "clockUpdate(\" "+fieldId+"\"); ";
setInterval(clo ckUpdate(fieldI d),1000);
Here, you call the clockUpdate function right now, and try evaluating
the result of that in one second. Try:
setInterval(fun ction(){clockUp date(fieldId);} ,1000);
or use the above codeSnippet with: //setInterval(cod eSnippet,1000); //setInterval("ev al(\"" + codeSnippet + "\")",1000) ;
Don't use eval. Don't ever use eval (the exceptions are so rare that
you'll probably never hit them).
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
Yes, the return value of clockUpdate is undefined. It is useless to
delay "undefined" for one second.
The second/third approach yeild:

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


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2688
by: Robert Mark Bram | last post by:
Howdy All! I would like to find since when did different versions of browsers and/or JavaScript/JScript/ECMAScript support the SetTimeout and SetInterval methods.. this is for compatibility reasons. Thanks for any advice! Rob :)
1
8913
by: John | last post by:
Michael Winter <M.Winter@blueyonder.co.invalid> wrote in message news:<opr3wd8yvj5vklcq@news-text.blueyonder.co.uk>... > On 24 Feb 2004 14:13:47 -0800, John <johnmark@fastermail.com> wrote: > > > I would like to use setTimeout and window.open methods to pop up > > window every 5 minutes. I can only manage to make the window to pop up > > only twice. > > > > function openWindow() { > > window.open("popupwindow.htm",...
5
2579
by: oliver | last post by:
Hi, the structure of my source code is like this: <script> C = function(){ //some initialization } C.prototype.start = function{ //some action
15
3248
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone tell me why these DIVs don't drift to the left as they are supposed to? <script language="javascript">
18
2203
by: Frances Del Rio | last post by:
this code is supposed to flip imgs at intervals of one second, but it flipps them much faster, it seems it flips them all in that first second, the more seconds the faster it flips them, instead of the other way around... (I mean it seems to me the more seconds I put in there the more seconds should go by betw. flips... each photo is in a div, each div has a z-index of 0, 1, 2, etc..) http://www.francesdelrio.com/cassini (pls reload a...
9
6726
by: pengypenguin | last post by:
As we know, Javascript suffers an insufferable lack of a wait() or sleep() function, and so setTimeOut must step in to take up the slack. A function can use setTimeOut like sleep() by calling itself after x seconds. This begs a few questions: Is this design building enormous calling stacks to nowhere? Will the users machine eventually run out of memory space to contain this stack? How can this be avoided? Thanks, -- Whit Nelson
15
3796
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first image rotates. I cannot figure out how to accomplish the 3 second delay. My code is pasted below: function randPic(){ randPic1(); randPic2();
1
2402
by: Terry | last post by:
Hi folks. I tried to use setInterval like this but it did not work setInterval ( removeitem (theitem) { theitem.style.display= "none"; }, 1000 ); the function without the setInterval is
1
2106
by: shawnwperkins | last post by:
Hi Guys, I'm new to Javascript and have a couple of questions that I hope someone could quickly answer for me. First, I'm trying to modify an example Ajax script to fit my needs. The script is located here: http://ajaxify.com/run/time/periodicRefresh/
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.