Using Javascript To Read And Display A Txt File Dynamically
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 cont
Solution 1:
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:
Solution 2:
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);
Solution 3:
Your ajax results might be cached try setting $.ajaxSetup({cache: false})
. Also I'm not sure what you are trying to achieve with the setTimeout
s, are you trying to load the page after 3+1 seconds?
<script type="text/javascript">
$.ajaxSetup({cache: false})
setTimeout(read, 3000);
function read(){
jQuery.get('now.txt',function(data){
document.write(data);});
}
</script>
Post a Comment for "Using Javascript To Read And Display A Txt File Dynamically"