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

154
Vistas
How to make timed requests to a server to avoid rate-limiting using Javascripts setTimeout?

I am trying to use setTimeout in a for loop so that my HTTP requests get sent once per second to avoid rate-limiting. However, it doesn't seem to be working. Could someone please help?

async function initiateSearchExperimental() {
  const json = await getCollections();

  for (let i = 0; i < json.result.data.length; i++) {
    setTimeout(getData(json, i), 1000 * i)
  }
}

function getData(json, i) {
  fetch(`https://howrare.is${json.result.data[i].url}/?for_sale=on&sort_by=rank`).then(function(response) {
    // The API call was successful!
    return response.text();
  }).then(function(html) {

    // Convert the HTML string into a document object
    var parser = new DOMParser();
    var doc = parser.parseFromString(html, 'text/html');
    var priceArray = getPriceArray(doc.querySelectorAll("div.featured_item"))

    console.log(json.result.data[i].url, curateArrayTwo(priceArray, json.result.data[i]))

  }).catch(function(err) {
    // There was an error
    console.warn('Something went wrong.', err);
  });
}

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

0

No need for await and such

I suggest you DO use setTimeout, but do it in the success of the second fetch

let data;
let cnt = 0;
const getData() {
  if (cnt >= data.length) return; // stop  
  fetch(`https://howrare.is${data[cnt].url}/?for_sale=on&sort_by=rank`)
    .then(function(response) {
      // The API call was successful!
      return response.text();
    }).then(function(html) {
      // Convert the HTML string into a document object
      var parser = new DOMParser();
      var doc = parser.parseFromString(html, 'text/html');
      var priceArray = getPriceArray(doc.querySelectorAll("div.featured_item"))
      console.log(data[cnt].url, curateArrayTwo(priceArray, data[cnt]))
      cnt++
      setTimeout(getData, 1000)
    }).catch(function(err) {
      // There was an error
      console.warn('Something went wrong.', err);
    });
};


fetch(collectionurl)
  .then(response => response.json())
  .then(json => {
    data = json.result.data;
    getData()
  });
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