我有一个页面正在重新加载 div 中的数据,我正在使用 jquery 来更新 div,但它只触发一次。
我的页面如下所示:
<html><head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
</head><body>
<div id="messages" style="width:100%;font-size:120%;">
<?php echo $tbl; ?>
</div>
<script type="text/javascript">
$(document).ready (function () {
var updater = setTimeout (function () {
$('div#messages').load ('new_hud.php?update=true');
}, 2000);
});
</script>
</body></html>
我希望脚本每 2 秒加载一次数据。它最初会在前 2 秒后重新加载数据,但之后就不再加载了。我在这里缺少什么?
最佳答案
将 setTimeout
替换为 setInterval
,如下所示
var updater = setInterval(function () {
$('div#messages').load ('new_hud.php?update=true');
}, 2000);
关于javascript - Jquery Ajax 只能运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31879803/