By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Send variable to setTimeout

P: n/a
Howdy All!

How can I make something like this work?

var message = "This will appear in 10 seconds";

setTimeout ('alert(message)', 10000);

Thanks for any advice!

Rob
:)
:>
:]
Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Robert Mark Bram" <re********@remove.this.optusnet.com.au> wrote in message
news:op*****************@news.optusnet.com.au...
Howdy All!

How can I make something like this work?

var message = "This will appear in 10 seconds";

setTimeout ('alert(message)', 10000);

Thanks for any advice!

Rob
:)
:>
:]


You've got it; however, the message should reflect that it appeared "after"
ten seconds not "in" ten seconds. Try the following as-is; watch for
word-wrap.
<html>
<head>
<title>timeout.htm</title>
<script language="javascript" type="text/javascript">
<!--
var message = "This will appear after 10 seconds";
setTimeout ('alert(message)', 10000);
// -->
</script>
</head>
<body>
</body>
</html>
Jul 20 '05 #2

P: n/a
Better yet:

var message = "This page loaded 10 seconds ago.";
Jul 20 '05 #3

P: n/a
Robert Mark Bram wrote:
How can I make something like this work?

var message = "This will appear in 10 seconds";

setTimeout ('alert(message)', 10000);


If you want to use this in a function, consider the following:

function alertmsg(message) {
alert(message);
// Escape single quotes in message
message = message.replace(/'/g,"\\'");
setTimeout ("alertmsg('" + message + "')", 10000);
}

Otherwise, follow McKirahan's advise...
JW

Jul 20 '05 #4

P: n/a
"Janwillem Borleffs" <jw@jwscripts.com> writes:
Robert Mark Bram wrote:
How can I make something like this work?

var message = "This will appear in 10 seconds";

setTimeout ('alert(message)', 10000);
If you want to use this in a function, consider the following:

function alertmsg(message) {
alert(message);
// Escape single quotes in message
message = message.replace(/'/g,"\\'");
setTimeout ("alertmsg('" + message + "')", 10000);
}
That fails in several ways for
alertmsg("I'm happy because my backslash (\\) is slanted!\nYes I am");
You handle the ', but the "\" is interpreted as an escape and is lost
(because '\)' becomes ')', and the newline is also used literally,
giving a syntax error, since newlines are not allowed inside string
literals..
Otherwise, follow McKirahan's advise...


That will work, but requires a global variable.

There is one other way, that sadly only works in IE from version 5.5:
using a function as argument to setTimeout:
---
function alertmsg(message) {
alert(message);
// Escape single quotes in message
setTimeout (function(){alertmsg(message);}, 10000);
}
---
This constant recalling, which was not in the original posters code,
is better handled with setInterval anyway:
---
function startalerts(message) {
return setInterval(function(){alert(message);},10000);
}
---

The equivalent for the original poster's problem is:

var message = "whatever";
setTimeout(function(){alert(message);},10000);
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
Lasse Reichstein Nielsen wrote:
That fails in several ways for
alertmsg("I'm happy because my backslash (\\) is slanted!\nYes I
am"); You handle the ', but the "\" is interpreted as an escape and
is lost (because '\)' becomes ')', and the newline is also used
literally, giving a syntax error, since newlines are not allowed
inside string literals..


Well, that's easy enough to fix:

function alertmsg(message) {
message = unescape(message);
alert(message);
setTimeout ("alertmsg('" + escape(message) + "')", 10000);
}
JW

Jul 20 '05 #6

P: n/a
Lasse Reichstein Nielsen wrote:
There is one other way, that sadly only works in IE from version 5.5:
using a function as argument to setTimeout:


Mozilla/4.0+ supports that as well, it even supports passing non-string
arguments to the called function, passing them as third, fourth aso.
argument of window.setTimeout(...).

http://devedge.netscape.com/library/...w.html#1203758
PointedEars
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.