使用ajax轮询做实时数据更新
1 <?php 2 header('content-type:text/html;charset=utf-8'); 3 $op = isset($_POST['op']) ? $_POST['op'] : ''; 4 if ($op == 'getdata') { 5 $url = "http://market.huobi.com/staticmarket/detail.html"; 6 $content = file_get_contents($url); 7 $content = str_replace("view_detail(", "[", $content); 8 $content = str_replace(")", "]", $content); 9 10 echo $content; 11 //$data = json_decode($content, true); 12 exit; 13 } 14 ?> 15 <div id="abc"></div> 16 <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> 17 <script> 18 setInterval("test()",2000); 19 function test() { 20 $.ajax({ 21 type: "POST", 22 url: "answer.php", 23 timeout: 60000, 24 async: true, 25 data: "op=getdata", 26 success: function(data, textStatus) { 27 $("#abc").html(data); 28 } 29 }); 30 } 31 </script>
1、将需要刷新的页面数据片段单独做出来
2、在原始页面中使用ajax轮询,每2秒请求一次,将请求得到的页面片段放入原始页面中对应的位置
3、调整完善页面样式
浙公网安备 33010602011771号