2017-03-09 47 views
-1

我正在制作一个网页,我想要在div标签中加载php文件的内容。例如,每隔5秒刷新一次div内容并加载一个php,在我的主页面上更新它自己,而不用重新加载我的主页面。主网页越来越长,因为div标签中的内容在特定时间间隔后重新加载

我的问题是,当定时器重新加载div内容时,我的主页面变得越来越垂直,并且减慢了网页的速度。

我试图加载此方式:使用 脚本是PHP,jQuery的,HTML

function Load_external_content(){ 
    $('#chatsidebar').empty(); 
    $('#chatsidebar').load('chatcnew.php'); 
} 

setInterval('Load_external_content()', 5000); 
<div class="chat-sidebar" id='chatsidebar'> 
    <?php include("chatcnew.php"); ?> 
</div> 

从我chatcnew.php文件中的数据正确加载在我的div,但发生在滚动我的主页,我不知道发生了什么。

+1

如果您正在构建聊天系统,我强烈建议您使用网络套接字。所有轮询都是DDOS你自己的服务器。 –

回答

0

尝试setTimeout,而不是因为加载HTML成本不确定的时间。

function Load_external_content(){ 
    $('#chatsidebar').empty(); 
    //you can do optimizing: render chatcnew.php is not necessary if the data of the chatcnew.php not change 
    $('#chatsidebar').load('chatcnew.php',function(){ 
     setTimeout(Load_external_content,5000); 
    }); 
} 
Load_external_content();