I was trying to read a txt file and display its content in my web page, since its content changes over time, I want to update it periodically. Here is my code, it displays the content at first, but it won't change after I changed the file's content. Any suggestions? Thanks.
<script type="text/javascript">
setTimeout(read(),3000);
function read(){
setTimeout(jQuery.get('now.txt',function(data){
document.write(data);}),1000);
}
</script>
Nearly there. Change:
setTimeout('read', 3000);
^^^^^ here
and here:
function read(){
jQuery.get('now.txt',function(data){document.write(data);});
}
If you want it to refresh every 3 seconds use setInterval
Documentation:
the function name does not need to be closed. It also does not need to be a string.
change this
setTimeout(read(),3000);
to this
setTimeout(read, 3000);
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