Newbie question; refresh with interval

D

DJ Craig

I'm new to JavaScript. I have a checkbox with a text field next to it.
When the checkbox is checked, I want the page to automatically refresh
with a time interval. The time interval will be equal to the number
entered in the text box (in milliseconds). When I run this code, it
always crashes the browser (so I'm assuming that it's trying to refresh
the page with no time delay). What's wrong with this code?

<html>
<head>
<script type="text/javascript">
var loc = location;
var delaytime = 5000;
while(delaytime>=500)
setTimeout("location=loc;",delaytime);
</script>
</head>
<body>

<script type="text/javascript">
function togglerefresh(checked){
if(checked)
delaytime = 0;
else
delaytime = getElementById('delay').value;
}
</script>

<form action="index.php" method="post">
<input type="checkbox" onclick="ref=true;"
onchange="togglerefresh(this.value);" title="Refresh automatically" />
<input type="text" value="5000" name="delay"
onblur="delaytime=this.value;" size="6" maxlength="6" title="Delay
between refreshes" /> milliseconds
</form>

</body>
</html>
 
S

Stephen Chalmers

DJ Craig said:
I'm new to JavaScript. I have a checkbox with a text field next to it.
When the checkbox is checked, I want the page to automatically refresh
with a time interval. The time interval will be equal to the number
entered in the text box (in milliseconds). When I run this code, it
always crashes the browser (so I'm assuming that it's trying to refresh
the page with no time delay). What's wrong with this code?

<html>
<head>
<script type="text/javascript">
var loc = location;
var delaytime = 5000;
</script>
</head>
<body>

<script type="text/javascript">
function togglerefresh(checked){
if(checked)
delaytime = 0;
else
delaytime = getElementById('delay').value;
}
</script>

<form action="index.php" method="post">
<input type="checkbox" onclick="ref=true;"
onchange="togglerefresh(this.value);" title="Refresh automatically" />
<input type="text" value="5000" name="delay"
onblur="delaytime=this.value;" size="6" maxlength="6" title="Delay
between refreshes" /> milliseconds
</form>

</body>
</html>
while(delaytime>=500)
setTimeout("location=loc;",delaytime);

This is not a practical technique and I would guess that this is the cause
of the crash. The execution of such a loop prevents execution of other
instructions in the code, preventing the loop terminating.
You need to store the value returned by setTimeout(), which then you can use
either to extend a pending delay with another call to setTimeout(), or
cancel it with clearTimeout().
<input type="checkbox" onclick="ref=true;"
onchange="togglerefresh(this.value);" title="Refresh automatically" />

Here you are passing the value of the checkbox element (which is not
assigned) instead of its checked status.

Use: togglerefresh(this.checked) - after you've re-written the function.

[Please note that there may be a timing issue regarding the point at which
the checked status of a check box changes, relative to the event used to
read its value. This was discussed recently, but I don't recall the exact
outcome.]
delaytime = getElementById('delay').value;

This is not the best way to read a form element, and in this case presumably
would fail, as you have not assigned an ID to the element.

As this could well be a homework assignment, I shall leave you to your
research.
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top