Managing numerous inquiries from a single customer within a succession of brief intervals

After creating a web application with a dashboard to showcase different reports and graphs based on user selections, I encountered an issue.

Users can interact with the reports using checkboxes and radio buttons. Every time a checkbox or radio button is selected, an ajax call is made to display the corresponding report.

The problem arises when users rapidly click on multiple checkboxes or radio buttons in quick succession. The server receives and responds to these requests in the same order they were sent, causing reports to overwrite each other almost instantly.

For instance, if there are two radio buttons - one for a pie chart and another for a line graph - and a user clicks on both rapidly, the pie chart will be displayed first but quickly replaced by the line graph.

Is there a way to prioritize the latest request over all earlier ones?

Important Points:

  1. I aim to avoid continuous server hits while complying with the client's wish of not having a submit button.
  2. It would be ideal to implement a javascript/ajax solution that only sends the most recent request to the server.
  3. My technology stack includes Java/J2EE Struts2 framework, along with javascript and ajax.

Answer №1

Describe a time range, essentially a lengthened, handmade event-loop to group or transmit only the most recent request:

var timerID

function inputChangeHandler() {

  clearInterval(timerID)

  timerID = setTimeout(function() { 
    // transfer information to server
  }, 1000);

}

This is just one of various potential methods, in this instance the server will only accept data after a period of 1000ms has passed without any activity.

Answer №2

There are two approaches to solving this issue that I believe are worth considering.

The first method involves introducing a slight delay, which has been suggested by other users.

  • Pros: Prevents the initial request from being sent, reducing server load.
  • Cons: Causes a delay in processing requests, leading to potential user wait times even for individual requests.

The second approach is to cancel the first request upon receiving a subsequent one.

  • Pros: Ensures real-time handling of requests.
  • Cons: Results in every request being sent to the server, potentially increasing server load.

Below is a code snippet that showcases how the second option could be implemented. It may require some adjustments as it's not tested extensively, but it captures the core concept:

var xhr = null; // tracks the current request

function sendRequest(){

    if(xhr && xhr.readyState != 4){
        // If a new request is made while another is still pending, abort the previous one
        xhr.abort(); 
    }

    xhr = $.ajax({
        .
        .
        . // Specify request parameters   
        done: function(data){
            // process the response data
        }
    });
}

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

Please consider opening in a new tab rather than a new window

<a href='#' onclick="loadpage();">[RANDOM PAGE]</a> Whenever this code is clicked, the following function gets executed. function loadpage(){ $.ajax ({ type: "POST", url: "fetchpage.php", data: "showpage=1", success: function(msg) { ...

Begin by introducing a fresh attribute to a JSON entity

Looking for help with modifying JSON data: var myVar = { "9":"Automotive & Industrial", "1":"Books", "7":"Clothing" }; I need to insert a new element at the beginning of the array, resulting in this: var myVar = { "5":"Electroni ...

React: Updating State with the Spread Operator and Array Method to Add Elements to an Array

Link to my jsfiddle code snippet: https://jsfiddle.net/ilikeflex/r2uws1ez/30/ I've been attempting to update the state in React by accessing the previous state. There are three different cases where I'm trying to achieve this, but I'm havin ...

`In NodeJS, facing a challenge with implementing a many-to-many relationship using Sequelize's

I'm in the process of setting up many-to-many relationships between roles and accesses. The `roles` table will contain a list of roles (admin, developer, etc...) and the `accesses` table will have a list of permissions (Create Project, Create Site, De ...

Verify whether the labels are blank, remain as they are, or transfer the data to the database

Currently, I am utilizing PHP, HTML5, and JavaScript for my project. The task at hand involves creating a webpage that will be connected to a database. However, I have encountered an issue wherein the page proceeds to the next step and sends data even when ...

Load HTML table values dynamically with Ajax post page load in PHP

My goal is to retrieve the connectivity status of available servers in a database on a PHP page. <tbody> <?php foreach ($data['servers'] as $server) { ?> <tr> <td class=""><?php echo $server->server_ ...

Reduce the amount of time it takes for a Google AdWords Script to generate a

According to Google Script best practices, it is recommended to store operations in an array and then call the methods once all the operations have been constructed. This helps minimize response time each time a service is called. For example, let's ...

ERROR: Module 'jquery' not found in path 'C:....' when using Gulp and Browserify

Encountering an error: Error: Module 'jquery' not found in path 'F:...\newstyle\assets\lib\helper\html\img\js' at C:\Users...\AppData\Roaming\npm\node_modules&bs ...

Optimal Strategies for Handling CSRF Tokens with AJAX Requests in Laravel 9 and Beyond

When working with Laravel 9+, it is crucial to expose CSRF tokens for AJAX requests in order to maintain security measures. However, the placement of these tokens can impact code organization and elegance. There are two main approaches: Approach 1: Direct ...

A guide on executing multiple Post Requests in Node.js

Currently, I am facing some issues with my code while attempting to make multiple post requests based on certain conditions. The goal is to retrieve data from an online database (Firebase), save it locally, and then delete the online data. Here's wha ...

Changes on services do not affect the Angular component

Currently facing an issue with my Angular assignment where changing an element's value doesn't reflect in the browser, even though the change is logged in the console. The task involves toggling the status of a member from active to inactive and ...

Having trouble retrieving the value of the hidden field using ng-model

Hello, I'm currently learning AngularJS and facing some challenges with accessing hidden field values using ng-model. Specifically, I am working on an editing modal where I need to retrieve the ID for each record. Below is my controller code snippet: ...

Tips for retrieving the HTML file of a modified canvas in HTML5

I’m currently working on a project to develop a website that allows users to design their own pages by simply dragging and dropping elements onto the canvas. The goal is for users to be able to save their creations as an HTML file. I’m facing challen ...

The Xpath used to locate the newsfeed box on Facebook is malfunctioning

I am currently attempting to accomplish the following tasks using Selenium WebDriver with Java: Logging in to Facebook Clicking on the username Posting the word "How are you!!" as a newsfeed update While the first two steps are successful, the third ste ...

Angular's implementation of a web socket connection

I am facing an issue with my Angular project where the web socket connection only opens upon page reload, and not when initially accessed. My goal is to have the socket start as soon as a user logs in, and close when they log out. Here is the custom socke ...

The setInterval() function is not functioning properly when used with PHP's file_get_contents

Currently, I'm encountering an issue while attempting to use the function get_file_contents() within a setInterval(). The objective is to continuously update some text that displays the contents of a file. Below is the code snippet: <script src="h ...

Troubleshooting problem with Shopify mailto tag

I am facing an issue with external links in my Shopify store. My app injects a script to display a bubble with an anchor tag that redirects users to a specified link. However, Shopify is altering the anchor tag to a different link, resulting in a 404 erro ...

Angular6 HttpClient: Unable to Inject Headers in Get Request for Chrome and IE11

Under my Angular 6 application, I am attempting to make a GET request while injecting some custom Headers: Here is how my service is structured: @Injectable() export class MyService { constructor(public httpClient: HttpClient) { } getUserInfos(login): Ob ...

Removing the Shadow from Material UI's Dialog Box

I am currently struggling with the Material UI dialog component that displays information about a location marker. My issue is with disabling the unattractive box shadow that surrounds the component. Despite setting box-shadow: none and trying different so ...

Use jQuery to remove an ID from an element using AJAX to interact with a database, then update

Hello, I am currently using jQuery to retrieve ids of multiple fields with ajax and send the data to be deleted via php. Despite being able to delete one item successfully, I am struggling to remove other ids. For example: Within a for loop that retrieves ...