This has been well answered several times, but being new to js I must be missing something basic.
My simple chat page works well, but refreshes only on manual call, and I want it to auto-refresh.
The php fetches the comment via POST, writes it to the log file (in the same directory), then writes the updated log file to the div.
<div id="show"><?php include 'LOG.txt' ; ?></div>
I adapted the following code from another post and tried it in header and in body, but it is not working.
<script type="text/javascript">
function doRefresh(){
$("#show").load("LOG.txt");
}
setInterval(function(){doRefresh()}, 5000);
</script>
What does this need?
Thank you.
答案 0 :(得分:1)
您的代码大多是正确的。但是,如果代码位于标题中,则需要确保仅在DOM就绪时才启动该函数。当然,你必须包含jQuery。
注意:如果您将代码放在public <R, S, CR extends Collection<R>, CS extends Collection<S>> CR foo(CS c);
之前,它应该可以正常工作。
</body>
答案 1 :(得分:0)
尝试通过Ajax
function doRefresh(){
$.ajax({
url : "helloworld.txt",
dataType: "text",
success : function (data) {
$(".text").html(data);
}
});
setTimeout(function() {
doRefresh();
}, 2000);
}
$(document).ready(function () {
doRefresh();
});
检查控制台是否有错误。
答案 2 :(得分:0)
setInterval()
方法或窗口关闭之前, clearInterval()
方法将继续调用。
setTimeout()
方法在指定的毫秒数后调用函数。
我过去常常将此类案件视为以下示例:
function doRefresh()
{
$("#show").load("LOG.txt");
setTimeout(function() {
doRefresh();
}, 2000);
}
$(document).ready(function () {
doRefresh();
});
通过调用函数本身,就像这个例子一样,使用setTimeout()
方法,它将每2秒调用一次。
查看我的 jsfiddle 版本