Click to See Complete Forum and Search --> : settimout() question.


vicpal25
10-17-2005, 05:40 PM
I have the following:


var t = setTimeout('alert("This is where the HTML goes")', 5000);


i want this to display on a <p> tag after the timer has ran out..how do i target that..do i create a different function?

Charles
10-17-2005, 05:45 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

<script typoe="text/javascript">
function helloWorld () {
var text = document.createTextNode ('Hello, World!')
var p = document.createElement ('P')
p.appendChild (text)
document.getElementsByTagName ('BODY')[0].appendChild (p)
}

if (document.getElementById) onload = function () {setTimeout ("helloWorld()", 5000)}
</script>

</head>
<body>

</body>
</html>

vicpal25
10-17-2005, 05:52 PM
doesnt work dude. this just targets the first <p> tag right? how would you target by a <p id="text"> with that id tag..

konithomimo
10-17-2005, 05:57 PM
var t = setTimeout('alert(document.getElementById('text').value="Whatever text you want!!!")', 5000);

vicpal25
10-17-2005, 06:09 PM
okay so in your function what do i replace..i cant get the id to call it out..this is what i have so far

<HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

<script typoe="text/javascript">
function helloWorld () {
document.getElementById('text').value="Whatever text you want!!!";
}

if (document.getElementById) onload = function timer() {setTimeout ("helloWorld()", 5000)}
</script>

</head>
<body onload="helloWorld()">
<p id="text"></p>
</body>
</html>
</HTML>

konithomimo
10-17-2005, 06:14 PM
I can tell that you don't know how to code, since you are just copying and pasting a bunch of things together. It should look like this:


<HTML>
<head>
<script type="text/javascript">
function MyTimer () {
setTimeout('alert(document.getElementById('text').value="Whatever text you want!!!")', 5000);
}
</script>
</head>
<body onload="MyTimer()">
<p id="text" value=""></p>
</body>

</HTML>


Or like you said, call another function:

<HTML>
<head>
<script type="text/javascript">
function MyTimer() {
setTimeout("otherfunction()",5000);

}

function otherfunction()
{
document.getElementById('text').value="Whatever text you want!!!";
}
</script>
</head>
<body onload="MyTimer()">
<p name="mytext" id="text"></p>
</body>

</HTML>


EDIT - Decided that you don't need alert since you dont know what to do . . . the second code snippet in this post should work for what you want to do.

vicpal25
10-17-2005, 06:19 PM
nahh bro all it gives me its an alert..it doesnt render to the <p id="text">..i tried taking the alert but it gives me a ; since the brackets are missing.

Charles
10-17-2005, 06:26 PM
You have two options. Note carefully, if the P element in question allready contains a text node then you can simply overwrite the character data associated with that text node:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

<script typoe="text/javascript">
if (document.getElementById) onload = function () {setTimeout ("document.getElementById ('hello-world').firstChild.data = 'Hello, World!'", 500)}
</script>

</head>
<body>
<p id="hello-world">&nbsp;</p>
</body>
</html> But if the P element is empty then you have to append a text node:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

<script typoe="text/javascript">
if (document.getElementById) onload = function () {setTimeout ("var text = document.createTextNode ('Hello, World!'); document.getElementById ('hello-world').appendChild (text)", 500)}
</script>

</head>
<body>
<p id="hello-world"></p>
</body>
</html>