0

I have a Javascript loop function

 firebase.auth().onAuthStateChanged((user) => {
  if (user) {
        database = firebase.database();

      var BusinessesId = firebase.auth().currentUser.uid;
      var ref = database.ref('/Jobs/');
    ref.on('value', ApplicationData,  errData);

  }
  })

         function JobData(data) { 

              var container = document.getElementById('Jobs'); 

              data.forEach(function(JobSnap) { // loop over all jobs
                var key = JobSnap.key;
                var Jobs = JobSnap.val();
                var newCard = `
                       <div class="thumbnail" id="${key}">
                           <span class="folder"><span class="file"></span></span>
                           <div class="title" id="Jobs">${Jobs.JobTitle}</div>
                        </div>
                    `;
              container.innerHTML += newCard;
              console.log(key);
              })
            }

that displays this enter image description here

What I noticed is that whenever I leave the page on for some hours, the folders would have been multiplied. is like the everything is duplicated again. The longer I left it opened the more the retrieved data duplicates itself. for example, if I left the webpage opened and walked away for about an hour or so by the time i come back their will be two duplicates of each folder showed below. What is causing this and how can I stop it?

1 Answer 1

1

You are calling function on this event, so my guess, state changes again and call is made again.

firebase.auth().onAuthStateChanged()

and then you append thumbnails to container

container.innerHTML += newCard;

you can reset the content like this

var container = document.getElementById('Jobs'); 
container.innerHTML = '';

or look into why event .onAuthStateChanged() is fired again.

Sign up to request clarification or add additional context in comments.

2 Comments

where exactly are you suggesting that I put container.innerHTML = '';
right after var container = document.getElementById('Jobs');, just like i have in the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.