【发布时间】:2016-03-30 08:33:52
【问题描述】:
我有一个在 html 中显示分数的内容。这是一个例子
<div id="homescore">0</div>
<div id="awayscore">0</div>
我希望在没有 php 的情况下每 5 秒自动刷新 div homecore 和 awayscore。有没有可能
【问题讨论】:
标签: javascript jquery refresh
我有一个在 html 中显示分数的内容。这是一个例子
<div id="homescore">0</div>
<div id="awayscore">0</div>
我希望在没有 php 的情况下每 5 秒自动刷新 div homecore 和 awayscore。有没有可能
【问题讨论】:
标签: javascript jquery refresh
您可以使用setTimeout 函数来做到这一点。
定义一个函数refreshDivContent,它将获取需要刷新的 div id。从 ajax 调用或您正在执行的任何源填充 div(我已经展示了一个 ajax 示例),然后在 @ 内再次调用此 refreshDivContent 987654324@.
$(document).ready(function(){
var refreshDivContent = function(divid) {
$.ajax({
url: "path",
cache: false,
success: function(data) {
$('#' + divid).html(data);
setTimeout(function() {
refreshDivContent(divid);
}, 5000);
}
});
};
refreshDivContent('homescore'); // Use it
});
【讨论】: