doo6568 2011-03-24 21:33
浏览 53
已采纳

通过jQuery.load从数据库中刷新AJAX Div

I have written a plugin for Wordpress which generates a jQuery.accordion list of content that it pulls from a database. I also have it pulling newer results from the database every 5 seconds via a separate page (update.php) through the jQuery.load method.

The problem I'm encountering is that I'm generating the full HTML for the div in the update.php file. It seems like I could just make it query the database and then return the results to the main file, but I'm not sure how to go about doing that. Basically, my concern here is efficiency, because with the way I'm doing things it may be considerably taxing on browser resources. I have a hunch that memory usage will continue to climb with this method until it inevitably peaks and the browser crashes.

I'm looking for any guidance on how to better code this. I've provided proof-of-concept examples below, so I'd love for anyone to offer feedback, whether constructive or critical. Thank you!

main.php:

<script type="text/javascript">
$(function() {
  $("#accordion").load("update.php");
  var refresh = setInterval(function() {
    $k("#accordion").load("update.php");
  }, 5000);
});
</script>

<div id="accordion"></div>

update.php:

<?php
function page_update() {
  global $wpdb;
  $out = '';

  $out .= '<script type="text/javascript" src="jquery-1.5.min.js"></script>';
  $out .= '<script type="text/javascript" src="jquery-ui-1.8.11.custom.min.js"></script>';
  $out .= '<script type="text/javascript">
           var $j = jQuery.noConflict();
           $j(function() {
             $j("#accordion").accordion();
           });
           </script>';

  $sql = "SELECT * FROM table ORDER BY name DESC;";
  $results = $wpdb->get_results($sql);

  foreach($results as $res) {
    $out .= '<h3>'.$res->name.'</h3>';
    $out .= '<div id="content-'.$res->name.'">'.$res->score.'</div>';
  }

  echo $out;
}

page_update();
?>
  • 写回答

2条回答 默认 最新

  • duanhui1869 2011-03-24 22:51
    关注
    1. You should not use the global keyword. Why?

    2. Before you redraw all your accordion div, you should make a function that check if there is any changes in the database. If there is no changes, their is no need to redraw.

    3. If there is a change, you could redraw only the affected row.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵