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>
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>
Share
Improve this question
asked Aug 21, 2012 at 20:38
user1519773user1519773
412 silver badges6 bronze badges
3 Answers
Reset to default 3Nearly 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./js/js_timing.asp
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);
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>