简体   繁体   English

jQuery div刷新包含的PHP文件

[英]jquery div refresh with included PHP files

I am using this code to constantly refresh divs with PHP included files in: 我正在使用此代码来不断刷新php中包含的文件的div:

$(document).ready(function() {
setInterval(function() {
     $('#ContentLeft').load('live_stats1.php').fadeIn("slow");
     $('#ContentRight').load('live_stats2.php').fadeIn("slow");
}, 2000);

});

my HTML looks like: 我的HTML看起来像:

<div id="ContentLeft">
<?php include 'live_stats1.php'; ?>
</div>

<div id="ContentRight">
<?php include 'live_stats2.php'; ?>
</div>

When i look in the console in Google Chrome is shows the live_stat1.php and live_stats2.php pages constantly loading, will this cause problems as they are constantly refreshing and slow down the internet/use a lot of bandwidth on the internet connection it is running on? 当我在Google Chrome浏览器的控制台中看到live_stat1.php和live_stats2.php页面不断加载时,这会引起问题,因为它们不断刷新并降低了Internet的速度/正在运行的Internet连接上使用大量带宽上?

It depends on the amount of processing that is being done in live_stats1.php and live_stats2.php I'm guessing because you are loading stats of some kind you may be doing quite a few database queries in those scripts. 这取决于live_stats1.phplive_stats2.phplive_stats1.php的处理量,因为您正在加载某种类型的统计信息,因此您可能在这些脚本中进行了很多数据库查询。

I think 2 second is a very short interval and I would think about increasing this. 我认为2秒是一个非常短的间隔,我会考虑增加此间隔。 Do someone benchmarking on how long those scripts take to complete and find the optimum interval time. 有人对这些脚本完成需要多长时间进行基准测试,并找到最佳间隔时间。

As you are using .load() you could think about adding a callback when its complete and set a variable. 当您使用.load()您可以考虑在完成回调时添加一个回调并设置一个变量。 Then on each loop your could check to see if the previous load has finished. 然后在每个循环上,您可以检查以前的加载是否已完成。 Because there is no point trying to keep loading a script that hasn't had time to finish 因为试图继续加载没有时间完成的脚本没有任何意义

var contentLeftLoaded = true;
var contentRightLoaded = true;
setInterval(function() {

    //Has content loaded from previous request
    if(contentLeftLoaded) {
        contentLeftLoaded = false;
        $('#ContentLeft').load('live_stats1.php', function() {
            $('#ContentLeft').fadeIn("slow");
            contentLeftLoaded = true;
        });
    }
    if(contentRightLoaded) {
        contentRightLoaded = false;
        $('#ContentRight').load('live_stats2.php', function() {
            $('#ContentRight').fadeIn("slow");
            contentRightLoaded = true;
        });
    }
}, 2000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM