The average duration for each API request is consistently recorded at 21 seconds

It's taking 21 seconds per request for snippet.json and images, causing my widget to load in 42 seconds consistently. That just doesn't seem right.

Check out this code snippet below:

<script type="text/javascript">
  function fetchJSONFile(path, callback) {
    var httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function() {
      if (httpRequest.readyState === 4) {
        if (httpRequest.status === 200) {
          var data = JSON.parse(httpRequest.responseText);
          if (callback) callback(data);
        };
      };
    };
    httpRequest.open('GET', path);
    httpRequest.send(); 
  }

  // This function fetches the JSON file and executes a callback once it's available
  fetchJSONFile('http://ecorebox.com/companies/1/snippet.json', function(data){
    // process the data
    var trees = data['trees'];
    var water = data['water'];
    var energy = data['electricity'];

    // Widget creation with dynamic content
    var widget = document.createElement("div");
    widget.id = 'erb_widget';
    widget.style.width = "200px";
    widget.style.height = "400px";
    // Adding more styles...

    // Appending elements to the widget
    $('.content').append(widget);
    document.getElementById('erb_widget').appendChild(logo);
    document.getElementById('erb_widget').appendChild(txt);
    // More appends...

  });
</script>

If you want to see how this works live, visit . Eco ReBox is a Rails application hosted on Heroku. Let me know if you need any specific code from there to better understand the situation.

Answer №1

Dealing with a comparable issue, I encountered lengthy 21-second API calls on a regular basis while using Windows 7. After some investigation, I pinpointed the problem to be related to proxy settings. By navigating to Internet Options/Connections/LAN Settings and deselecting 'Automatically detect settings', I saw a significant decrease in my API call time, down to just 700ms. It seems that there may have been a timeout occurring during the process of 'detecting settings'...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Optimal method for managing errors in Flask when handling AJAX requests from the front end

I'm currently working on a React application that communicates with a Python Flask server. One of the features I am adding allows users to change their passwords. To do this, an AJAX request is sent from React to Flask, containing both the old and ne ...

Include a series of key-value pairs in an existing list if the key is not already present

I have a dataset with JSON strings in a column for each row. To extract information, I convert them to lists and utilize json normalize to create a dataframe. Assuming 'extras' is the column name in the data frame, I first use the following quer ...

Resolving issues with jQuery's live() method while incorporating AJAX calls

One of the challenges I'm facing is with buttons on a webpage that are part of the class "go". The code snippet below demonstrates how I handle actions related to these buttons: $(".go").live('click', this.handleAction); The issue arises w ...

React frontend encountered a connectivity issue while trying to establish a network connection

Our app utilizes next.js connected to express, which is further linked to AWS MySql for database management. While attempting to load some products stored in the database, an error message pops up: TypeError: NetworkError when trying to fetch resource. ...

The categorizing and organizing of arrays

My goal is to create a messaging application where users can view a list of their recent messages, along with a preview of the latest message received/sent, before selecting a conversation. However, I'm facing an issue with my script that displays all ...

Retrieving the value of a <select> element using React.useState in a Nextjs environment

Encountering an issue in Nextjs involving the useState function from React. There is a select element with multiple options. Upon selection, the useState should store the value of the selected option. const [value, setValue] = useState('') ... ...

Choosing an element with JavaScript

var displayedImage = document.querySelector('.displayed-img'); var thumbBar = document.querySelector('.thumb-bar'); btn = document.querySelector('button'); var overlay = document.querySelector('.overlay'); /* Itera ...

if else within the <dd></dd> tags

I have a code snippet in Vue.js that displays a certain value. <dl> <!-- Fan speed --> <dt>{{ $t('pageInventory.table.fanSpeed') }}:</dt> <dd>{{ dataForma ...

Has anybody managed to successfully implement this require or debug NPM module for use in a web browser?

Has anyone successfully implemented the require or debug NPM modules in a browser environment? Despite claims and instructions on the debug NPM module's page that it can be used in the browser, attempting to do so results in the following error: Unc ...

Trigger event upon variable modification

Currently, I am working on a school project that involves creating a website where users can listen to music together. I have implemented a button that allows the user to listen to the same song another user is currently playing at the right position. Howe ...

How can we properly format a string to be included in a JSON string using Kotlin?

There is a challenge of receiving certain string that becomes invalid when converted to JSON, requiring it to be escaped. Is there a pre-existing function in Kotlin for escaping strings? ...

Deserialization of DateTime in RestSharp

I am encountering an issue with the default JSON deserialization in Rest Sharp. Here is the User class I am working with: public partial class User { public long Id { get; set; } public string Name { get; set; } public DateTime? Date { get; set; ...

Syntax for private members in ES6 classes

I'm curious to know the most efficient way to declare private members within ES6 classes. In simpler terms, what is the best practice for implementing: function MyClass() { var privateFunction = function() { return 0; }; this.publicFuncti ...

AngularJS not passing date data to web API

Greetings! I am currently working on a web application using AngularJS. I have a date value in AngularJS, for example 13-10-2017. In C#, I have the following field: public DateTime LicenseExpiryDate { get; set; } When I send 13-10-2017 in an AJAX reques ...

Modify the text displayed on the upload file button

I'm looking to modify the name of the file upload button in JSON, but I'm unsure of the process Here is the current Json script https://i.stack.imgur.com/pgbcQ.png This is the output of the current Json https://i.stack.imgur.com/NYWkv.png I ...

Error: the server encountered a problem and was unable to fulfill the request (500)

I am attempting to retrieve data from a web service in Json format. Below is the code I am using: WebClient wc = new WebClient(); wc.UseDefaultCredentials = true; var data = wc.DownloadString(JsonUri); MemoryStream ms = new Memory ...

React client side componentDidMount function encountering issues (server side rendering)

Greetings to the Stackoverflow community Apologies in advance if my explanation is not clear enough When a user directly types a URL, the server makes a request, fetches the corresponding data, and renders it on the screen flawlessly. However, when a us ...

You cannot convert a function to a string while utilizing axios get in nuxtServerInit

While attempting to connect my app to the backend using Udemy's Nuxt.js course, I encountered a GET http://localhost:3000/ 500 (Internal Server Error) on the client side with the following code: import Vuex from 'vuex'; import axios from &a ...

In JavaScript, the code is designed to recognize and return one specific file type for a group of files that have similar formats (such as 'mp4' or 'm4v' being recognized as 'MOV')

I am working on a populateTable function where I want to merge different file types read from a JSON file into one display type. Specifically, I am trying to combine mp4 and m4v files into MOV format. However, despite not encountering any errors in my code ...

Struggling with filtering an array fetched from an API using VueJS

Currently, I am working on a Nativescript-Vue app and facing some challenges that I need help with. The Scenario I have data coming in from an API. Here is the structure of the data I receive: { "count": 9, "results": [ { "id": 1, "c ...