Each subsequent ajax call is delayed by 300 milliseconds

Something unusual caught my attention while working with ajax calls. There seems to be a consistent 300ms delay in every other ajax call.

In the network section, you can see what the requests look like here. Upon examining the details of the calls, I noticed differences between a fast ajax call and a slow ajax call, particularly in two additional fields - DNS lookup and Initial Connection.

I'm puzzled by this inconsistency in performance. How can I ensure that all ajax calls deliver consistent results?

The test code:

<body>
    <input type="button" class="btn" id="testButton" value="Test"/>
</body>

<script>
    document.getElementById('testButton').onclick = function() {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                console.log('done');
            }
        };
        xhttp.open("GET", "{% url 'test_ajax' %}", true);
        xhttp.send();
    }
</script>

def test_ajax(request):
    return JsonResponse({'a': 'a'})

UPDATE: I even tried using jQuery for the ajax call, but the issue persists.

Answer №1

While some may disagree, I personally believe that conducting an AJAX request without the use of jQuery is not ideal. Perhaps exploring the following question will provide more insight:

Ajax tutorial for post and get

It seems like your view function is heading in the right direction.

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

Tips for inserting a button under a div when clicked

I am working on a layout where I have several div cards aligned side by side using the display: inline-block property. What I want to achieve is that when I click on a card, a button is added below the respective div. To accomplish this, I tried using jqu ...

What is the process for triggering events when the previous and next buttons are clicked?

Within the jQuery fullcalendar, there are previous and next buttons. Is there a way to trigger specific events when these buttons are clicked? ...

Recursive sorting and parsing of JSON data with multiple levels

I'm new to using recursion in JavaScript and need some guidance to understand it better. I have a JSON data structure with multiple levels of nested "subcategories". const STORE_CATEGORIES = [{ "Id":"1", "Name":"One Parent", ...

Sending data to HTML using parameters in a .cshtml document

I encountered a situation where I had to deal with the following code snippet within a .cshtml file: var peopleList = $('#PeopleListTable').dataTable({ // not relevant "fnRender": function (oObj) { ...

What could be causing the failure in revealing these elements?

Within my Meteor application, I utilize Template.dynamic to seamlessly replace the current Template with the next one. In my "main" html file, the setup looks like this: <div class="container"> {{> postTravelWizard}} </div> </body> ...

JavaScript: Accessing the selectedIndex of a dropdown list in RadGrid

Currently facing an issue with utilizing the RadGrid control from Telerik. I am attempting to access and manipulate the value of a GridDropDowncolumn within RadGrid using JavaScript (while in edit mode). Does anyone have any suggestions for the correct Ja ...

Generating a dynamic triangle within a div while ensuring it fits perfectly with the maximum width

I need help with creating a responsive triangle on a web page that takes three inputs. The challenge is to adjust the size of the triangle so it fits inside a div element while maintaining the aspect ratio of the inputs. For instance, if the inputs are 500 ...

jquery:Error: string literal not properly terminated

Every time I add a space between a word and click to call a function, I encounter the following error: SyntaxError: unterminated string literal [Break On This Error] $(this).SelectProjectBox(363,"ssss mypage# (line 1, col 29) HTML generated by the b ...

Strategies for optimizing progressive enhancement

Designing a website that is accessible to everyone is truly an art form, and Progressive Enhancement is something I hold dear... I am curious to hear about the best techniques you have used to ensure websites work for all users, regardless of their browse ...

Failed to insert a new element into the MongoDB array

After trying the code below to update a document within a collection, I encountered an issue where a new item was not being added to an array in the script. Despite no exceptions being thrown, the array remained unchanged. I am seeking advice from experts ...

Guide to updating the object value within an array in React

Is there a way to toggle the value of a specific object key called "clicked" to true using the spread operator in React? I want to be able to press a button and update the object's value. let questions = [ { title: "What do I want to learn ...

What is the best way to retrieve the title element from a loaded iframe?

Looking for a way to create a dynamic title for my iframe without manually coding it in. My goal is to extract the src of the iframe, load that object, and then retrieve the title element from it. I attempted using iframe.contentDocument but encountered ...

What is the best way to activate a hyperlink using jQuery without causing the page to reload?

I'm in the process of designing a tumblr theme and have added a like button to each post. A demo URL looks like this: <a class="loveit" href="http://www.tumblr.com/like/fGKvAJgQ?id=16664837215">like it</a> The current setup works fine, b ...

Having trouble retrieving the desired data from the JSON file

My current code is not giving me the expected results while trying to access JSON values with jQuery. Any suggestions on how I can resolve this issue? // JSON response: [{ "private": "8044553.0" }, { "governmentdocs": "98952.0" }, { "officiald ...

The 3D cube in Three.js gains momentum with each re-initialization, spinning faster and faster

I'm puzzled by a phenomenon with the first Three.js example: the spinning 3D cube spins faster and faster every time it is re-initialized. The code consists of an init function that sets up the scene and an animate function that kicks off the animati ...

Exploring hierarchical information within a JSON response from a Wiki API

As a newcomer to this field, I am currently exploring ways to access Wikipedia's API for extracting the value of "extract" and adding it to an HTML element dynamically. The challenge lies in the fact that the information is subject to change based on ...

Transferring data between databases in Django

I have encountered an issue with transferring data between two sqlite.db files. Specifically, I need to copy the contents of one column in a table from one db file to another. For example: The model Information is stored in a db file named new.db: class ...

Using Javascript to make a call to a RESTful endpoint

My current challenge involves attempting to make a call to the Spotify API from JavaScript: function callAPI() { var xhttp = new XMLHttpRequest(); xhttp.open('GET', 'https://api.spotify.com/v1/search?q=Muse&type=track'); ...

jQuery Form Validator: requiring double submission

I've been working on implementing the Form Validator using Jquery. After some testing, I noticed that I have to click the submit button twice before the validation kicks in. Why is that? The script is housed in a separate file and included in my proj ...

What is the best way to isolate the text before a particular segment within a string, excluding any blank spaces?

Is it possible to use vanilla JavaScript in order to extract the first string value directly to the left of a specified portion of a longer string, excluding any blank spaces? Consider the following string: 1 hr 30 min How could you extract the 1 located ...