使用ajax輪詢做實時資料更新
<?php
header( 'content-type:text/html;charset=utf-8' );
$op = isset( $_POST [ 'op' ]) ? $_POST [ 'op' ] : '' ;
if ( $op == 'getdata' ) {
$url = "http://market.huobi.com/staticmarket/detail.html" ;
$content = file_get_contents ( $url );
$content = str_replace ( "view_detail(" , "[" , $content );
$content = str_replace ( ")" , "]" , $content );
echo $content ;
//$data = json_decode($content, true);
exit ;
}
?>
<div id= "abc" ></div>
<script type= "text/javascript" src= "http://code.jquery.com/jquery-latest.js" ></script>
<script>
setInterval( "test()" ,2000);
function test() {
$.ajax({
type: "POST" ,
url: "answer.php" ,
timeout: 60000,
async: true,
data: "op=getdata" ,
success: function (data, textStatus) {
$( "#abc" ).html(data);
}
});
}
</script>
|
1、將需要重新整理的頁面資料片段單獨做出來
2、在原始頁面中使用ajax輪詢,每2秒請求一次,將請求得到的頁面片段放入原始頁面中對應的位置
3、調整完善頁面樣式