【问题标题】:Main Webpage getting longer and longer as the contents in a div tag are reloaded after a specific time interval随着 div 标签中的内容在特定时间间隔后重新加载,主网页变得越来越长
【发布时间】:2017-03-09 18:41:36
【问题描述】:

我正在制作一个网页,我想在其中加载 div 标记中的 php 文件的内容。例如,每 5 秒刷新一次 div 内容并加载一个 php 以在我的主页上进行自我更新,而无需重新加载我的主页。

我遇到的问题是,当计时器重新加载 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 中正确加载,但滚动发生在我的主页上,我不确定发生了什么。

【问题讨论】:

  • 如果你正在构建一个聊天系统,我强烈建议你使用网络套接字。轮询所做的只是 DDOS 你自己的服务器。

标签: javascript php jquery html css


【解决方案1】:

尝试 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2021-11-08
    相关资源
    最近更新 更多