首页 > 代码库 > 实时刷新

实时刷新

js

<script type="text/javascript" src="http://www.mamicode.com/jquery.js"></script>
<script>
$(function(){
    window.setInterval(function(){
        $.getJSON("json.php",function(data){
            document.getElementById(‘ceshi‘).innerHTML = data.name+data.price;
        });
    },1000);
});
</script>

<div id="ceshi"></div>

php

<?php

$price = rand(10,100);

echo "{
    \"name\": \"铜\",
    \"price\": \"".$price."\",
    \"diff\": \"++146\",
    \"diffRate\": \"++0.37%\",
    \"jk\": \"39487\",
    \"zk\": \"39445\",
    \"zg\": \"39977\",
    \"zd\": \"39127\",
    \"class\": \"39591\"
}";
?>

 

实时刷新