JQuery间隔不起作用

时间:2017-02-28 18:17:12

标签: javascript php jquery html ajax

我制作了一个小脚本,将页面加载到另一个页面的div中,每隔5秒刷新一次。

页面按预期加载,但间隔似乎不起作用。

我已经搜索了一个解决方案,但所有线程都说我的代码应该像那样工作。所以我决定在这里发布完整的代码。

JQuery(和AJAX):

<script>
function load(){
    $('#chatload').load('/intern/chat/chatlogframe.php/?name=muster',function () {
         $(this).unwrap();
    });
}

load(); // run it on pageload
setInterval(function(){
    load() // this should run every 5 seconds
}, 5000);
</script>

chatlogframe.php文件包含SQL Select查询。每次执行scipt时都应重新加载数据。

更新:

我在Chrome控制台中查看了Uncaught TypeError: $(...).unwrap is not a function

我不认为这个功能是错误的,但也许会有所帮助。

更新2:

继承人html div:

<div id='chatload'></div>

1 个答案:

答案 0 :(得分:0)

有效。可能存在$('#chatload').load('/intern/chat/chatlogframe.php/?name=muster',function () { $(this).unwrap(); });

中的代码存在问题

.unwrap()可能是问题,但要隔离您需要发布示例HTML容器。

&#13;
&#13;
function load() {
  console.log("Do something");
  $('#chatload').load('https://jsonplaceholder.typicode.com/posts/1', function() {
    $(this).unwrap();
  });
}

load(); // run it on pageload
setInterval(function() {
  load() // this should run every 5 seconds
}, 5000);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="chatload"></div>
&#13;
&#13;
&#13;