473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

setTimout

Within a function I would like to use a variable in the setTimout string,
for example:

var jHello = 'Hello';
setTimeout("alert(" + jHello + ")", 1000);

This gives jHello undefined error.

What is the correct syntax?

Thanking you in anticipation.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Mar 8 '06 #1
4 11057
VK

Roger Withnell wrote:
Within a function I would like to use a variable in the setTimout string,
for example:

var jHello = 'Hello';
setTimeout("alert(" + jHello + ")", 1000);

This gives jHello undefined error.

What is the correct syntax?


setTimeout("alert(jHello)", 1000);

setTimeout resolves variables while forming the callBack string. So
1sec later in your variant it will try to execute alert(Hello).

Please note that setTimeout("alert(jHello)", 1000); will fail if jHello
variable declared locally in a function: 1sec later it will not exist
anymore.

Mar 8 '06 #2
or you could evaluate the value of jHello (notice the extra quotes) and
not worry about the scope of jHello:

var jHello = 'Hello';
setTimeout("alert('" + jHello + "')", 1000);
// evaluates to: setTimeout("alert('Hello')",1000);

Mar 8 '06 #3
On Wed, 8 Mar 2006 14:04:11 -0000, Roger Withnell wrote:
var jHello = 'Hello';
setTimeout("alert(" + jHello + ")", 1000);

This gives jHello undefined error.


Actually it says that 'Hello' (not 'jHello') is undefined, because the
expression becomes setTimeout("alert(Hello)",1000) once the string is
evaluated. I think you meant:

setTimeout("alert('"+jHello+"')",1000);

....which becomes setTimeout("alert('Hello')",1000) once the string is
evaluated (the extra quote marks tell Javascript to treat the contents of
jHello as a string, not a variable).

--
Safalra (Stephen Morley)
http://www.safalra.com/programming/javascript/
Mar 8 '06 #4
VK wrote:
Please note that setTimeout("alert(jHello)", 1000); will fail if jHello
variable declared locally in a function: 1sec later it will not exist
anymore.


To solve this problem you can code up this way:

var x = "Joninhas Lalah";
setTimeout(function(){alert(x);}, 0);
--
Now with alcohol <URL:http://youtube.com/watch?v=lnQTZxqxc10> =X
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Mar 8 '06 #5

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

Similar topics

3
by: M Katz | last post by:
Hi, I'm using the following to allow dynamic resixing of an image (when the usre presses down on a button): function widthup() { image.width = image.width + 1; width.innerText = image.width;...
5
by: Richard A. DeVenezia | last post by:
Is some future Mozilla going to support setInterval ( <function:function>, <interval:number> ) ? Right now it seems to be simply setInterval ( <function-text:string>, <interval:number> ) --...
14
by: Brandon Hoppe | last post by:
I'm trying to change the src of an ilayer in the parent document from a link inside the ilayer. I'm not able to get it to work. All that happens is Netscape 4 crashes. This is for Netscape 4 only....
2
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script,...
9
by: Thomas Christensen | last post by:
If anyone can provide an explanation and/or workaround for this bug in IE I would be really grateful. Is this a known bug?: ******CODE******* <html> <script type="text/javascript"> function...
1
by: anagai | last post by:
hi I would like to cancel a settimout from happening. I dont want it to execute the expression at all. clearTimeout just cancels the delay but executes the expression anyway. I want to cancel...
27
by: rsteph | last post by:
I've got a site that appears properly in both IE and Netscape for has all sorts of appearance issues in Mozilla Firefox. I've looked around for some points on the web that help explain the issue, but...
3
by: jackwootton | last post by:
Hello, I have a for loop, which calls a method for (var i = 0; i < GmailXChat.gmailChatFrames.length; i ++) { // myObjectArray.constructIframe(); } the method 'constructIframe();'...
5
by: fjm | last post by:
Hi everyone, I usually hang out on the php side because its what I know, but I am faced with a problem that I honestly have no idea how to solve and figured I would try my luck over here. I am hoping...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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...

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.