Forum Moderators: open
<script type="text/javascript">
function change()
{
content = document.getElementById("content");
setTimeout('change()',10000);
}
</script>
</head>
<body>
<div class="main" style="border: 5px solid rgb(0, 0, 0);">
<div class="body">
<div id="content">test</div>
</div>
</div>
</body>
</html>
Alert for testing:
function change()
{
content = document.getElementById("content");
alert(content);
setTimeout('change()',10000);
}
One way to get the timer started:
<body onload="change();">
May I ask what the script is meant to accomplish? :)
I am just trying to learn how to manipulate dividers through javascript thats all to help me with maybe other things later.
When I add the alert I get a error on the page.
Just to add, the code that whoisgregg gives above is perfectly OK and the alert should work as given, but you may be getting confused by what is in the alert message, since your 'content' variable is an object, not a string... in IE it should simply say, "[object]". In FF, it should say, "[object HTMLDivElement]". This is not an error.
Alternatively, the alert() function could be called like:
alert('Hello World!');
Here's complete code working code that uses settimeout to run both a timer and to periodically update a div with the user's entry in a textarea... It's a bit hacked together but it works. Hopefully this will be helpful in seeing how it works. :)
<html>
<head>
<script type="text/javascript">
<!--
var time = 0;
var refresh_rate = 5;
function change()
{
var entry = document.getElementById("entry");
var content = document.getElementById("content");
var timerDiv = document.getElementById('timer');
if(time%refresh_rate == 0){
content.innerHTML = entry.value;
time = 0;
}
timerDiv.innerHTML = ''+parseInt( Math.abs( (time%refresh_rate) -refresh_rate))+' seconds before refresh';
time += 1;
setTimeout('change()',1000);
}
-->
</script>
</head>
<body onload="change();">
<div class="body">
<p>Enter HTML below:<br />
<textarea id="entry" style="width: 100%; height: 10em">test</textarea></p>
<div><span id="timer"></span> (refresh every
<a href="#" onclick="refresh_rate = 5; time=0; return false;">5</a>,
<a href="#" onclick="refresh_rate = 10; time=0; return false;">10</a>,
<a href="#" onclick="refresh_rate = 15; time=0; return false;">15</a>,
<a href="#" onclick="refresh_rate = 30; time=0; return false;">30</a>,
<a href="#" onclick="refresh_rate = 60; time=0; return false;">60</a> seconds)</div>
<hr />
<div id="content"></div>
</div>
</body>
</html>