Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Fix Fetch data form API and print it on HTML with JavaScript

I want to fetch data from API and show it on HTML by elementID with JavaScript, Please help what I wrong in this code below.

<div id="priceChange"></div>

<script>
setInterval(tradestime, 5000);
function tradestime(){
fetch("https://api.binance.com/api/v1/ticker/24hr?symbol=LTCUSDT") 
.then(
    (res) => {
        const data = res.json();
    }
)    
document.getElementById("priceChange").innerHTML = data.PriceChange;    
}
</script>

Please fix this code.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  1. You should use res.json() because you want data in JSON format. which will return promise and then you can get data. You should read fetch docs

    fetch("https://api.binance.com/api/v1/ticker/24hr?symbol=LTCUSDT")
     .then(res => res.json())
     .then(data => el.innerHTML = data.priceChange)
    
  2. key should be priceChange not PriceChange.

  3. You can use parseFloat + toFixed as:

    el.innerHTML = Number.parseFloat(data.priceChange).toFixed(2);
    

setInterval(tradestime, 5000);
const el = document.getElementById("priceChange");

function tradestime() {
  fetch("https://api.binance.com/api/v1/ticker/24hr?symbol=LTCUSDT")
    .then(res => res.json())
    .then(data => el.innerHTML = data.priceChange)
}
<div id="priceChange"></div>


You can also use async-await here

setInterval( tradestime, 5000 );
const el = document.getElementById( "priceChange" );

async function tradestime() {
  const res = await fetch( "https://api.binance.com/api/v1/ticker/24hr?symbol=LTCUSDT" )
  const data = await res.json();
  el.innerHTML = data.priceChange;
}
<div id="priceChange"></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda