Reload image without refreshing page

I have little experience with javascript, I’m trying to make the image update but I’m not succeeding. This image comes through an API

image here

<script src="">

<script type="text/javascript">

   window.onload = function(){

      setInterval("atualizarqr()",3000); // troca imagem a cada 3 segundos      


   function atualizarqr(){

      var img = document.getElementById('img').src;     

         document.getElementById('img').src = "";


<body onload="atualizarqr">
            <img src="" id="img"/>

>Solution :

A couple of issues with this code:

  1. You create an img variable but do not use it. You can just remove the variable declaration altogether
  2. You are passing in the function to setInterval but as a string, which is incorrect

Here is the updated code:

function atualizarqr() {
  document.getElementById("img").src = "";

window.onload = function () {
  // troca imagem a cada 3 segundos
  setInterval(atualizarqr, 3000);

Leave a Reply