简体   繁体   English

AJAX刷新区不起作用

[英]AJAX Refresh Div Not Functioning

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
    setInterval(function() {
        $.get('points.txt', function(data){
            $('#show').html(data);
        },1000);
    });
});
</script>

<div id="show"></div>

I have the above script running, and in points.txt there's a number which keeps changing. 我运行了上面的脚本,在points.txt中有一个不断变化的数字。

It's suppose to refresh the div every second. 假设每秒刷新一次div。

Now, for some reason, the script ain't working. 现在,由于某种原因,该脚本无法正常工作。 What am I doing wrong? 我究竟做错了什么?

Your second param of timing function is not given to the setInterval , and not the get . 计时功能的第二个参数未提供给setInterval ,而不是get Also I guess your request is getting cached. 另外,我猜您的请求已被缓存。 Try this: 尝试这个:

$(document).ready(function() {
  setInterval(function() {
    $.get('points.txt?' + (new Date).getTime(), function(data){
      $('#show').html(data);
    });
  }, 1000);
});

Or tell the AJAX, not to cache data: 或者告诉AJAX,不要缓存数据:

$.ajaxSetup({
  cache: false
});

I believe there is incorrect formatting of your code. 我相信您的代码格式错误。 Parameter 1000 is added as third parameter to get function. 将参数1000添加为第三个参数以get功能。 Instead it should be like this: 相反,它应该是这样的:

$(document).ready(function() {
    setInterval(function() {
        $.get('points.txt', function(data){
            $('#show').html(data);
        });
    },1000);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM