Forum Moderators: open

Message Too Old, No Replies

How to tell is this Timeout is working?

         

Simone100

1:42 pm on Dec 2, 2006 (gmt 0)

10+ Year Member



Hello, I have a div on a timer below, but I don't know how to test if it is working. It is updating every 10 seconds, but since the text is the same, "test" nothing appears to be happening when it probably is. How can I add something to this so I can see some action every 10 seconds. I need to keep the div as the thing that is updating every 10. Thank you very much~~

<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>

whoisgregg

10:04 pm on Dec 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to make sure it's working for testing purposes, just add in an alert. You'll also need to "kick start" the timer.

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? :)

Simone100

2:32 am on Dec 3, 2006 (gmt 0)

10+ Year Member



Thank you very much, do you or anyone have an alert that works, or help me adjust my code to accomodate it? When I add the alert I get a error on the page. I think my function is ok, because I looked up docoment.GetElementById through google and several examples said that was how to do it.

I am just trying to learn how to manipulate dividers through javascript thats all to help me with maybe other things later.

Simone100

6:19 am on Dec 3, 2006 (gmt 0)

10+ Year Member



I need help to know the div function is working, and the proper way to do an alert, without getting an error message.

Simone100

7:25 am on Dec 3, 2006 (gmt 0)

10+ Year Member



I found another way thanks.

penders

3:48 pm on Dec 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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!');

whoisgregg

7:03 pm on Dec 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry it took so long for me to get back to this thread... :/

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>