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

Issues have arisen due to server calls being affected by AngularJS routing

I have implemented AngularJS in a nodewebkit application. There are three main views: Home.html Conversation.html Login.html Upon login, the following code is executed: $state.go('home.main'); which triggers the following state c ...

Having trouble saving data in Django using ajax(fetch) - encountered an error

Trying to save data using ajax in a model that references other models Example: class Friend(models.Model): name = ... class Main(models.Model): name = .... friend = models.ForeignKey(Friend, on_delete=models.CASCADE) Data is fetched using a ...

Using Angular to open a modal by invoking the href function

Current Project Context I am currently following a tutorial on CRUD operations with DataTables, but I am using Asp.Net WebApi with Angular for this project. At step 9 of the tutorial, it introduces partial views for pop-up windows. However, instead of us ...

Steps for retrieving multiple documents from Firestore within a cloud function

In my cloud function, I have set up a trigger that activates on document write. This function is designed to check multiple documents based on the trigger and execute if/else statements accordingly. I have developed a method that retrieves all documents u ...

How can you determine the number of elements that match in two arrays?

As a coding newbie, I'm looking to create a JavaScript function that compares two arrays and assigns a score based on the number of matching elements. However, when I attempt to run it in Chrome's console, I receive an error message stating that ...

Typescript Tooltip for eCharts

I'm working on customizing the tooltip in eChart v5.0.2 using Typescript, but I'm encountering an error related to the formatter that I can't seem to resolve. The error message regarding the function keyword is as follows: Type '(param ...

What can be used instead of makeStyles in Material UI version 5?

My journey with Material UI version 5 has just begun. In the past, I would utilize makestyles to incorporate my custom theme's styles; however, it appears that this method no longer works in v.5. Instead of relying on {createMuiTheme}, I have shifted ...

Tips on removing properties from an object recursively according to their key/value pairs

My current task involves removing specific children of an object based on whether their "size" key is set to 0. To achieve this, I am utilizing the npm package directory-tree to generate a JavaScript object representation of a chosen directory. The stru ...

Creating a minigame using JQuery (Javascript)

I am currently developing a collection of mini-games for my big project. The approach I've taken in creating this mini-game is similar to one I used previously. However, unlike my previous version which only had one PuzzleContainer, this new iteration ...

Can you nest an if statement within another if statement within a return statement?

Is it feasible to include an if statement inside another if statement within a return function? I understand the standard syntax like: return ( <div> { myVar ? <Component/> : <AnotherComponent/> } </div> ...

What is the most efficient method to import multiple MaterialUI components using a shared namespace without requiring the entire library to be imported

In order to prevent name mixing with other libraries, I want my MaterialUI components to be under the same namespace. For example, ensuring that <Box> references <MaterialUI.Box> and not <SomeOtherLibrary.Box>. Although it seems like a si ...

What is the rationale behind jQuery.each not using Array.forEach when it is accessible?

After delving deep into the codebase of the underscore library, I came across an interesting discovery. It seems that _.each relies on an ECMAScript 5 API called Array.forEach whenever it is available: var each = _.each = _.forEach = function(obj, iterato ...

What is the process for including echo HTML field in the WooCommerce order view?

I have successfully added HTML input fields within functions.php. However, these echoed fields are not displaying in WooCommerce orders after placing an order. I am looking for a way to include the values and labels of these fields in the orders view wit ...

Automatically load content using AJAX when scrolling within a HTML5 segment

Recently, I've been developing a website that dynamically loads new content from a MySQL database as the user scrolls. However, I've encountered an issue where the new content is loaded even with minimal scrolling, rather than only when reaching ...

I have an inquiry regarding the live method

$('.classname').live('click',function() { some code; }); Would there be any performance issues if there are around 100 to 300 elements with this specific class? ...

Encountering a problem with Vite and PostCSS: "Assignment to invalid left-hand side"

Yesterday everything was running smoothly, but today I encountered an error message saying Invalid left-hand side in assignment. When I checked the Chrome console, it displayed the same error related to a file named ${mod.url} pointing to this source code: ...

Transfer the imageURI to a different HTML page

My mobile app, created using PhoneGap, allows users to select an image from their album. I want to pass that selected image and display it on another HTML page. Does anyone have any suggestions on how to achieve this? Below is the code snippet: selectImag ...

The $http Service encounters a failure with an unknown status code

Difficulty Integrating AngularJS, jQuery, and Adobe Panel Creation I recently updated the versions of AngularJS and jQuery for my project. Previously, I was using jquery-1.11.0.min.js and AngularJS 1.2.10. Now, I want to utilize the latest available versi ...

Troubleshooting a Custom Pipe Problem in Angular Material Drag and Drop

Currently, I am working on a project involving Angular Material Drag And Drop functionality. I have created a simplified example on StackBlitz which you can access through this link: here The project involves two lists - one containing pets and the other ...

Creating a Mongoose schema to store an array of objects, where updates will automatically add new objects

const mongoose = require('mongoose'); module.exports = mongoose.model('GridModel', { Request_Id : { type : Number, required : true }, viewStudents : { type : Array , default : [] } }); The mongoose model above needs to b ...