Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Javascript to read and display a txt file dynamically

Tags:

javascript

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>
like image 617
user1519773 Avatar asked Dec 13 '25 05:12

user1519773


2 Answers

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:

  • http://www.w3schools.com/js/js_timing.asp
like image 70
Peter Avatar answered Dec 15 '25 19:12

Peter


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);
like image 40
Taylor Hakes Avatar answered Dec 15 '25 19:12

Taylor Hakes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!