不断加载DIV? [英] Constant reload of a DIV?
问题描述
好的,在过去的4到5个小时里,我一直在Stack和其他地方逛逛,以寻求解决问题的方法.
Ok, I've looked around Stack and other places for the past 4 or 5 hours trying to find a solution to my problem.
我在一个包含5行信息的页面内有一个iframe,该信息是从数据库中获取的.这些信息会不断变化,因此需要每1-5秒刷新一次,如果需要,可以延长到10秒.
I have an Iframe inside of a page which contains 5 lines of information, the information is fetched from a database. Said info will constantly change, therefor a need it to be refreshed every 1-5 seconds, can stretch to 10 seconds if needs be.
我使用了下面的代码,该代码有效,但是由于某种原因使浏览器崩溃,我猜重新加载的速度太快了吗?
I have used the below code, which works, but crashed my browser(s) for some reason, I'm guessing reloading too fast?
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#info').load('mid.php');
}, 5000); // refresh every 5000 milliseconds
</script>
下面是mid.php中的代码(页面内有PHP,但这是我需要刷新的部分).
Below is the code from mid.php (There is PHP inside the page but this is the part that I need refreshing).
<div id="info"><font size="2">
<b>Crimes: </b><font color="white"><?php if ($fetch->lastcrime <= time()){ echo 'Ready'; }else{ echo "".maketime($fetch->lastcrime).""; } ?></font><br>
<b>GTA: </b><font color="white"><?php if ($fetch->lastgta <= time()){ echo 'Ready'; }else{ echo "".maketime($fetch->lastgta).""; } ?></font><br>
<b>Chase: </b><font color="white"><?php if ($fetch->last_chase < time()){ echo 'Ready'; }else{ echo "".maketime($fetch->last_chase).""; } ?></font><br>
<b>Extortion: </b><font color="white"><?php if ($fetch->last_ext < time()){ echo 'Ready'; }else{ echo "".maketime($fetch->last_ext).""; } ?></font><br>
<b>Rank:</b><?php echo "$fetch->rank"; ?></td></tr>
</div>
</table>
我知道我可以使用HTML刷新iframe,但是当屏幕的左上角每3秒刷新一次时,它看起来并不美观,
I know I can use HTML to refresh the Iframe but it looks unsightly when the whole top left corner of the screen refreshes every 3 seconds, any help is much appreciated.
谢谢
推荐答案
使用可以使用.load()
回调,用setTimeout()
代替setInterval
Use can use .load()
callback, substitute setTimeout()
for setInterval
$(document).ready(function() {
let timeout;
let duration = 5000;
let stop = false;
function update() {
$("#info").load("mid.php", function() {
if (timeout) {
clearTimeout(timeout)
}
if (stop === false) {
timeout = setTimeout(update, duration)
}
});
}
update();
});
这篇关于不断加载DIV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!