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>
var message = "This page loaded 10 seconds ago.";
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
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.'
Well, that's easy enough to fix:
function alertmsg(message) {
message = unescape(message);
alert(message);
setTimeout ("alertmsg('" + escape(message) + "')", 10000);
}
JW
> 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