最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Using Javascript to read and display a txt file dynamically - Stack Overflow

programmeradmin6浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 3

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./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 setTimeouts, 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>
发布评论

评论列表(0)

  1. 暂无评论