Send variable to setTimeout

R

Robert Mark Bram

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
:)
:>
:]
 
M

McKirahan

Robert Mark Bram said:
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>
 
J

Janwillem Borleffs

Robert said:
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
 
L

Lasse Reichstein Nielsen

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
 
J

Janwillem Borleffs

Lasse said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top