Addressing simple JavaScript "async" AJAX requests that are sequenced and reliant on each other

I'm currently developing an application that includes a dashboard on its main page. In order to address any potential loading issues, the content of the dashboard is being calculated asynchronously using Ajax. This means that users do not have to wait for the dashboard to fully load before navigating to another page or checking out other indicators.

Here's how it's supposed to work:

  • When the page loads, a javascript function triggered by onload() on the body makes an Ajax request to generate a canvas or skeleton for the dashboard with basic content for each component (like a loading gif and "Wait, loading content..." message), as well as hidden information such as refresh delays, wrapper div IDs, and component classes.
  • This Ajax call has an onSuccess part that triggers another function. This function looks for the hidden information and, for each component found, calls a third function to refresh its content (initially replacing the default content).
  • In summary, if my dashboard has 4 components, when I visit the page, an Ajax request creates a canvas, then scans the canvas to fire off 4 separate Ajax requests to calculate the content for each component.

Typically, since these calls are asynchronous, I can click on a link and go to another page without having to wait for all the calls to finish loading. The components also update quickly because each individual Ajax call is asynchronous. However, whenever I click on a link, it waits for all the calls to finish processing before moving to the new page, sometimes causing a delay in loading time. Additionally, the "refresh" calls are handled sequentially rather than concurrently.

How can I achieve true asynchrony? Initially, I tried specifying "asynchronous: true" in the Ajax calls but it didn't make a difference. We primarily use IE7 in "quirks mode" (IE8) due to issues with the doctype from the original developers. Below is the code snippet:

function create_dashboard() {
    // Code here
}

function tallyRefreshes() {
    // Code here
}

function refreshComponent(id_wrapper, component_class) {
    // Code here
}

The components currently refresh one after the other instead of concurrently, resulting in a lack of user control until all components have finished loading. How can I ensure truly asynchronous behavior?

Thank you in advance.

Answer №1

Update on the resolved issue: Although it didn't originate from the server, I discovered that it was related to sequentiality. The Ajax calls were utilizing the PHP session, and I found out that it is controlled by a file, causing the session to be "locked" and inaccessible for another page if it is already being accessed with writing permissions.

Since I was only using it as a cache for the current user profile (checking if it exists, defining it if not, and retrieving it), I included session_write_close() after the final writing access to release the session. This allowed all my calls to occur in a more simultaneous manner, as they could commence after the session usage rather than waiting for the entire script to finish execution (which could take up to 5 seconds for some components). Additionally, I could navigate to other pages (which also required the session) and experience smooth transitions without any noticeable delays. I appreciate the server suggestion, even though it wasn't the root cause, as it guided me 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

Determining the quantity of displays

Can we detect the number of screens in use using JavaScript or jQuery? We are working on an application that should not be displayed on multiple screens, even if the hardware allows for it. Appreciate any assistance with this matter. Thank you. ...

Is there a way to incorporate a variable into a JSON URL?

I'm attempting to incorporate a variable I have defined into the JSON URL: var articleName = "test"; $.getJSON( "https://www.googleapis.com/customsearch/v1?key=API_MY&cx=CX_MY&q='+articleName+'&searchType=image&fileType= ...

Utilize jQuery and JSP (or PHP) to showcase detailed information about a specific item when a user clicks on it within an HTML page

For my upcoming college project, I am tasked with developing a movie library. The main page of the library will feature various movie posters. Upon clicking on a poster, users will be directed to a dedicated page displaying detailed information about the ...

Issue with running the Jquery each function within a textbox inside an ASP.NET gridview

Below is the gridview markup: <asp:GridView ID="gvDoctorVisits" runat="server" DataKeyNames="AdmissionId" class="tableStyle" AutoGenerateColumns="False" Width="100%" EmptyDataText=& ...

Issue with clicking a button in Selenium using JavaScript for automation

I'm encountering an issue where Selenium is detecting an element as disabled, despite it being enabled. To work around this, I am attempting to click on the element using JavaScript with the following code snippet: IWebElement button = driver.FindEl ...

Is it possible to determine which child element is currently in view within a scrollable parent div?

In an attempt to replicate a "current page" feature using divs, similar to a PDF reader. document.addEventListener("DOMContentLoaded", function(event) { var container = document.getElementById("container"); container.onscroll = function() { let ...

Drawing intersecting lines on a javascript canvas and clearing them using the lineto method

I am working with a canvas where lines are drawn based on mouse movement. I am trying to achieve the effect of the line only lasting for a few seconds before disappearing, similar to swirling a ribbon with a set length. I have been using lineTo to draw the ...

Can the mDNS string received through webRTC be decoded to retrieve a user's IP address?

After doing some thorough research, I came across this insightful response from a different Stack Overflow question. The problem at hand involves retrieving an mDNS string, which looks like this: abcd1234-1e1e-1e1e-1e1e-abcd1a2bc3de.local I have a genuin ...

How to move a div beneath the JavaScript files in Drupal 7

I am facing a challenge where I need to position a div right above the body tag without interfering with the scripts located above it. Despite my efforts, I have not been successful in achieving this goal. I attempted to use Hook_page_build to position t ...

Is it possible to use Javascript to retrieve a variable from a remote PHP file?

I am trying to retrieve a variable from a remote PHP file using JavaScript in my phonegap application. The same origin policy doesn't apply here, so I believe I need to use JSON/AJAX for this task. However, I have been unable to find any tutorials tha ...

Analyzing XBRL documents using JavaScript

Looking to extract information from XBRL files like the one found here, I came across a promising npm module called parse-xbrl that claims to be capable of parsing XBRL files. Here is the code snippet I used for implementation: var ParseXbrl = require(&ap ...

What is the step-by-step process for chaining ajax requests using $q.deffer?

I have a task that requires the browser to make N requests to the server, where the requests must be synchronous and start only after the previous request has completed. One way to achieve this is by writing a function with a for loop and recursively call ...

Encountered a type error while attempting to render and define table columns within a material table component in

Hey there! I'm currently using the Material table to create a table view, and here are the columns that I have defined: const columns: TableColumn[] = [ { title: 'TYPE', field: 'type_of_action', highligh ...

What is the best way to send an array of data to a controller?

Hey there! I'm new to this community and could use some help with my code. Currently, I am working with Laravel 5.1 and Blade templates. Here is the snippet of code that's giving me trouble: function getNumber(){ values = []; values[ ...

Expanding properties in a React component based on certain conditions using TypeScript

I am attempting to dynamically expand my component props based on whether a specific prop is included. The goal is to add attributes from an anchor if the href prop is provided, and include attributes from a button if it is not. Is this achievable? Chec ...

The proper way to retrieve data using getServerSideProps

Encountering an issue with Next.js: Upon reaching pages/users, the following error is displayed: ./node_modules/mongodb/lib/cmap/auth/gssapi.js:4:0 Module not found: Can't resolve 'dns' Import trace for requested module: ./node_modules/mon ...

Dynamic form name validation in Angular is crucial for ensuring the accuracy and

When it comes to validating a form in Angular, I usually use the ng-submit directive like this: <form name="formName" ng-submit="formName.$valid && submitForm()"></form> This method works well for forms with predefined names that I se ...

Using different CSS classes interchangeably in AngularJS

Imagine you have a list with an unknown number of items, ranging from one to potentially dozens. You want to display five CSS classes in a regular alternating pattern. What would be the most effective approach to achieve this? Here is some sample HTML cod ...

When adding files through drag and drop, the FormData is including a blank file field in the sent

I am currently working on a photo upload page that has drag and drop functionality enabled. Below is the form code: <form id="Upload" method="post" action="sessionapi/UserPicture/Upload" enctype="multipart/form-data"> <input class="box__file ...

The product image does not display any other images

Hey, I'm experiencing an issue and need help replicating the photo changing effect on this website: I've managed to do everything right except for the photo changing feature (1 / 2 / 3 / 4)!! Here's what I have so far: Can anyone assist m ...