Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
Javascript API call causes freezing in my web application

During an API url call in my web application, my html page is crashes unexpectedly and throws a 'out of memory' error. I'm really not sure what the problem is. I am using xhr object to fetch data from server. I tested multiple hotel search api from rapid api but every single of them caused the same problem. My codes are :

JAVASCRIPT ONE

const btn4 = document.querySelector('#hotelBtn')

btn4.addEventListener('click', function showHotels(e) {

  var userQuery = $('#hotelQuery').val();


  const data = null;

  const xhr = new XMLHttpRequest();

  xhr.addEventListener("readystatechange", function () {
        if (this.readyState === this.DONE) {
              var hotelObj = JSON.parse(this.responseText)
              console.log(hotelObj)


              var myContainer = document.getElementById('query_results')

              for (let i = 0; hotelObj.length; i++) {

                    var myCol = document.createElement('div')
                    myCol.setAttribute('class', 'col')

                    var myRow = document.createElement('div')
                    myRow.setAttribute('class', 'row')



                    myRow.append(myCol)
                    myContainer.append(myRow)


              }





        }
  });

  xhr.open("GET", "https://booking-com.p.rapidapi.com/v1/hotels/locations?locale=en-gb&name=" + userQuery);
  xhr.setRequestHeader("x-rapidapi-host", "booking-com.p.rapidapi.com");
  xhr.setRequestHeader("x-rapidapi-key", "mykey");

  xhr.send(data);

  e.preventDefault()

})

HTML ONE

<div id="query_results">


                        <!-- Filled by Javascript -->





    
  </div>

Out of memory error : enter image description here

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Looks like you have an infinite loop in your code.

You will want to check that the loop ends based on the size of the length of the array...

for (let i = 0; i < hotelObj.length; i++) {

about 4 years ago · Juan Pablo Isaza Denunciar

0

In the mentioned code, the condition in the for loop implemented will be true for all iterations and hence the issue occurs. The solution provided by @inki is the right solution since the condition block checks whether i<length, which will be false at the end of array and exits from the loop. Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

about 4 years ago · Juan Pablo Isaza Denunciar

0

I have replaced traditional for loop with foreach loop structure. The problem seems solved right now. In tradition for loop, the loop may executing infinite times which is causing out of memory exception

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda