2017-08-10 120 views
0

我嘗試使用jQuery來刷新div。自動刷新一個div HTML/jQuery

<div class="col-lg-8" id="btc"> Send <?php echo $bitcoin_price; ?></div> 

<script> 
    $(document).ready(function() { 
     var interval = setInterval(refresh, 5000); 
    }); 

    function refresh() { 
     $.get('site', function (result) { 
      $('#btc').html(result); 
     }); 
    } 
</script> 


$bitcoin_price: 

$url = "https://bitpay.com/api/rates"; 

$json = file_get_contents($url); 
$data = json_decode($json, true); 

// $data[1] = USD 
$rate   = $data[1]["rate"]; 
$usd_price  = 10;  # Let cost of elephant be 10$ 
$bitcoin_price = round($usd_price/$rate, 8); 

任何人知道如何獲取該div刷新有間隔和執行請求?

或者這是不可能的?我是否需要包含並使用ajax刷新文件?

回答

1

此PHP代碼必須從AJAX調用:

$url = "https://bitpay.com/api/rates"; 

$json = file_get_contents($url); 
$data = json_decode($json, true); 

// $data[1] = USD 
$rate   = $data[1]["rate"]; 
$usd_price  = 10;  # Let cost of elephant be 10$ 
$bitcoin_price = round($usd_price/$rate, 8); 

你必須從你的refresh函數調用它,那簡直會做的工作。