Задать вопрос
Ответы пользователя по тегу JavaScript
  • Как можно ограничить скорость запроса в js?

    @available Автор вопроса
    Дабы не поднимать новую тему, хотелось бы понять есть ли решение для задержки на получение? Выше 2 варианта для пост запросов, мне же понадобилось и для гет запроса.

    Я честно и упорно и со всех сторон подходил к чат жпт, но он ничего вменяемого не смог предложить, все его ответы что то в стиле этого

    const url = 'https://site. com';
    const chunkSize = 500; // Number of bytes to read in each chunk
    const interval = 100; // Interval in milliseconds between each chunk
    
    fetch(url)
      .then(response => {
        const reader = response.body.getReader();
        let bytesRead = 0;
    
        function readChunk() {
          reader.read()
            .then(({ done, value }) => {
              if (done) {
                console.log('Finished reading the response.');
                return;
              }
    
              const chunk = value.slice(0, chunkSize);
              bytesRead += chunk.length;
    
              // Process the chunk as needed
              console.log(`Received ${chunk.length} bytes:`, chunk);
    
              // Schedule the next chunk read
              setTimeout(readChunk, interval);
            })
            .catch(error => {
              console.error('Error reading the response:', error);
            });
        }
    
        // Start reading the response in chunks
        readChunk();
      })
      .catch(error => {
        console.error('Error fetching the URL:', error);
      });
    Ответ написан
    Комментировать