I would like to use Javascript to auto-refresh my webpage while users can choose to turn on/turn off the auto-refresh function with two different buttons (Please be aware that the buttons are using <label>
and <input>
tag). I have tried my best to write the code but I still do not know how to link up these two codes so that it can function correctly.
Also, if user choose the buttons of auto-refresh ON, I want to keep auto refresh continuously after its first load instead of just auto refreshing only once.
Would you please help me to correct my codes, please? Thank you.
The code for ON and OFF buttons (two individual buttons):
<div class="page-header-actions" data-toggle="buttons" role="group">
<label class="btn btn-outline btn-primary active">
<input type="radio" name="options" autocomplete="off" value="autorefreshoff" checked />
<i class="icon wb-check text-active" aria-hidden="true"></i> Auto Refresh OFF
</label>
<label onClick="startrefreshon" class="btn btn-outline btn-primary">
<input type="radio" autocomplete="off" value="autorefreshon" />
<i class="icon wb-check text-active" aria-hidden="true"></i> Auto Refresh ON
</label>
</div>
The code for auto-refresh function:
<script type='text/javascript'>
setTimeout(function(){
window.location.reload(1);
}, 5000);
</script>
Here are a few things you need to do:
input type="radio"
should have same name
.auto-refresh-checkbox
).checked
code:
function reloadPage(){
var refreshEnabled = document.getElementById('auto-refresh-checkbox');
if(refreshEnabled.checked) {
window.location.reload(1);
}
}
setInterval
setInterval(reloadPage, 5000);
PS: The checked "auto refresh" radio will lose it's value on page reload, so you might have to save the value using localStorage or something else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With