Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
Why is my javascript while loop not working?

My while loop should, on each iteration, create an img tag, set its src, then add an event listener. This event listener fires on error event, which would be a 404, in the case the is not found. The images are stored in a local file, but I don't know how many files might be in each project folder. Images will be named 1, 2, 3, 4, etc. As soon as an error event has fired on one of the img tags, this will mean we have reached the end of the images for a particular project, and this event listener will change isLooping to false, and the while loop should stop executing at this point. However my browser just enters a state of perpetual loading. No errors in the console but browser will eventually crash. Why is this? I think it's a problem with how I have set up my while loop.

const GalleryProject = (function () {
  const galleryContainer = document.querySelector('.gallery-container')
  let isLooping = true

  function openProject(index = 1) {
    const imagesContainer = document.createElement('div')
    imagesContainer.classList.add('images-container')

    const bigImgTag = document.createElement('img')
    bigImgTag.classList.add('big')

    const smallImgTagContainer = document.createElement('div')
    smallImgTagContainer.classList.add('small-img-container')

    imagesContainer.appendChild(bigImgTag)
    imagesContainer.appendChild(smallImgTagContainer)
    galleryContainer.appendChild(imagesContainer)

    displayProjectInfo(index)
    loadBigImage(index)
    loadSmallImages(index)
  }

  function loadBigImage(index) {
    const bigImg = document.querySelector('img.big')
    bigImg.src = `assets/projects/single-bucket/${index}/main-image.jpeg`
  }

  const smallImgErrorHandler = function () {
    isLooping = false
    this.remove()
  }

  function loadSmallImages(index) {
    const smallImgContainer = document.querySelector('.small-img-container')
    let i = 1
    while (isLooping) {
      const img = document.createElement('img')
      img.addEventListener('error', smallImgErrorHandler)
      img.src = `assets/projects/single-bucket/${index}/${i}.jpeg`
      smallImgContainer.appendChild(img)
      i++
    }
  }

  function displayProjectInfo(index) {
    const textWrapper = document.createElement('div')
    textWrapper.classList.add('project-description-wrapper')

    const title = document.createElement('p')
    title.classList.add('project-title')
    title.textContent = projectData[index - 1].title

    const description = document.createElement('p')
    description.classList.add('project-description')
    description.textContent = projectData[index - 1].description

    textWrapper.appendChild(title)
    textWrapper.appendChild(description)
    galleryContainer.appendChild(textWrapper)
  }

  return { openProject }
})()

GalleryProject.openProject()
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This is not the correct way to program in JavaScript.

The loading of the images is going to be asynchronous, therefore, you need to wait for the load to be complete before you go on.

In other words, right now, you loop really fast adding new <img src=.../> tags without waiting for the image to be loaded or for the load to have failed. You need to do it asynchronously. This means create one image tag, wait for either success or failure. On success, try to load the next image. That means your code needs to change dramatically. The "while" loop is replace by these success/failure callbacks instead.


Update based on comments:

If you can instead get the total number of images for that one run, then having a loop based on that total number will be faster. This is because you'll give the browser all the URLs at once and in most cases it will attempt to load at least two images in parallel.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!