Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Send variable to setTimeout

9 views
Skip to first unread message

Robert Mark Bram

unread,
Dec 30, 2003, 9:40:47 PM12/30/03
to
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
:)
:>
:]

McKirahan

unread,
Dec 30, 2003, 10:44:31 PM12/30/03
to
"Robert Mark Bram" <relax...@remove.this.optusnet.com.au> wrote in message
news:opr00xp9e...@news.optusnet.com.au...

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>


McKirahan

unread,
Dec 30, 2003, 10:47:16 PM12/30/03
to
Better yet:

var message = "This page loaded 10 seconds ago.";


Janwillem Borleffs

unread,
Dec 31, 2003, 1:32:23 AM12/31/03
to
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

Lasse Reichstein Nielsen

unread,
Dec 31, 2003, 6:42:31 AM12/31/03
to
"Janwillem Borleffs" <j...@jwscripts.com> writes:

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 - l...@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Janwillem Borleffs

unread,
Dec 31, 2003, 7:20:22 AM12/31/03
to
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

Thomas 'PointedEars' Lahn

unread,
Jan 10, 2004, 11:59:04 PM1/10/04
to
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/manuals/2000/javascript/1.3/reference/window.html#1203758


PointedEars

0 new messages