JQ ajax间隔刷新 显示最新更新数据
帮朋友搞一个免刷新的页面,但是要求后台更新数据,前台要实时更新才行;这样的要求第一想到的就是JQ了!别的不说 直接上代码吧!半夜了,也累了!
JS代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<script> $(function(){ function autoPlay(){ var tableid = $('#tableid').val(); //用一个 input 记录ID $.ajax({ "url":"/index.php?m=content&c=index&a=jsonlist", "type":"post", "dataType":"json", "data":{ id:tableid }, "async":false, "success":function(data){ $.each(data.list,function(i,result){ if(i==0){ //记录数据库中最大ID值 赋值给 INPUT $('#tableid').val(result.id); } item = '<tr class=""><td>'+result.shijian+'5</td><td> '+result.title+' </td><td><span class=" ball_s_ ball_s_blue ball_lenght5 " title="'+result.wan+'">'+result.wan+'</span><span class=" ball_s_ ball_s_blue ball_lenght5 " title="'+result.qian+'">'+result.qian+'</span><span class=" ball_s_ ball_s_blue ball_lenght5 " title="'+result.bai+'">'+result.bai+'</span><span class=" ball_s_ ball_s_blue ball_lenght5 " title="'+result.shi+'">'+result.shi+'</span><span class=" ball_s_ ball_s_blue ball_lenght5 " title="'+result.ge+'">'+result.ge+'</span></td><td class="count">'+result.count+'</td><td class="gray">'+result.isdan+'</td><td class="blue">'+result.isxiao+'</td><td class="gray">'+result.longhu+'</td></tr>'; $('#reslist').append(item); }); } }); } setInterval(autoPlay,5000); //每隔5秒 刷新一次 }); </script> |
下面是PHP代码 用的是PHPCMS 随便搞了下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
//JSON 输出 自定义 public function jsonlist() { $id = intval($_POST['id']); $where = ''; if($id){ $where = ' and a.id>'.$id; } $sql = "SELECT * FROM `v9_ticai` a,`v9_ticai_data` b WHERE a.id=b.id AND a.status=99 $where order by a.id desc"; $qsql = $this->db->query($sql); $list = $this->db->fetch_array(); foreach($list as $vo){ $data['count'] = $vo['wan']+$vo['qian']+$vo['bai']+$vo['shi']+$vo['ge']; $data['wan'] = $vo['wan']; $data['qian'] = $vo['qian']; $data['bai'] = $vo['bai']; $data['shi'] = $vo['shi']; $data['ge'] = $vo['ge']; $data['isdan'] = $vo['isdan']; $data['isxiao'] = $vo['isxiao']; $data['longhu'] = $vo['longhu']; $data['title'] = $vo['title']; $data['shijian'] = $vo['shijian']; $data['id'] = $vo['id']; $array[] = $data; } echo json_encode(array('list'=>$array,'code'=>'1','sql'=>$sql)); } |
到此结束 此方法可能不是好的,如果您有更好的方法可以留言交流!