[英]How can I automatically update ethereum price on my website
I want to display the last price of etherum on my website but unfortunately it doesn't update automatically without reloading the page.我想在我的网站上显示以太坊的最新价格,但不幸的是它不会在不重新加载页面的情况下自动更新。
<script>
$.ajax({
url : 'https://api.coinmarketcap.com/v1/ticker/ethereum/',
type : 'GET',
data : {
'numberOfWords' : 10
},
dataType:'json',
success : function(data) {
eth_price = data[0].price_usd;
document.getElementById('eth_price').innerHTML = eth_price;
},
error : function(request,error)
{
console.log('Error by getting the ETH price');
}
});
</script>
How can I fix this so the last price is always displayed without reloading the site every time?如何解决此问题,以便始终显示最后价格,而无需每次都重新加载网站?
I thought ajax is doing that by default?我认为 ajax 默认是这样做的?
The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds). setInterval() 方法以指定的时间间隔(以毫秒为单位)调用函数或计算表达式。
setInterval(function(){
$.ajax({
url : 'https://api.coinmarketcap.com/v1/ticker/ethereum/',
type : 'GET',
data : {
'numberOfWords' : 10
},
dataType:'json',
success : function(data) {
eth_price = data[0].price_usd;
document.getElementById('eth_price').innerHTML = eth_price;
},
error : function(request,error)
{
console.log('Error by getting the ETH price');
}
})
}, 3000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.