The sluggishness of Ajax - A recursive problem

I've come across an issue with the ajax code I'm using to request a page. It's causing high memory consumption, slowing down the browser, and making everything lag. It seems like there's recursion happening but I'm not sure how to stop it. Here's a snippet of the problematic code:

$(".item").each(function() {
    $this = $(this);
    var dataString = {s: "<?echo $_SESSION['currentview_'.$stamp]?>", r:"<?echo $search_usernumber?>", st: "<?echo $stamp?>"};
    $.ajaxSetup({cache:false});
    function timeLeft() {
        $.ajax({
            type: "POST",
            url: "get_content_home.php",
            dataType: "html",
            data: dataString, 
            success: function(result) {
                $this.html(result);
                //console.log("a");
                window.setInterval(function() {
                    timeLeft();
                }, 500);
            }
        });
    }
    timeLeft();
});

Any ideas on how to fix this issue would be greatly appreciated. Thanks in advance.

Answer №1

You are in a recursive loop and should avoid using nested setInterval. This method will lead to an excessive number of interval instances. Instead of utilizing setInterval, it is advised to schedule additional requests using setTimeout.

setInterval will continuously fire at specified intervals until manually stopped.

setTimeout fires only once.


Let's take a look at the following code, which aims to resolve some of the issues seen in this question as well as your other two questions.

Firstly, as mentioned earlier, refrain from using setInterval unless you intend for it to run indefinitely. Furthermore, avoid nesting the creation of setInterval unless specifically required.

Instead, let's develop a recursive function called getTimeLeft() that will manage sending the request and scheduling the next time check after a certain duration.

This example also simulates the $.ajax() function so you can observe the function in operation without having access to an actual backend.

// Simulated server-side data for tracking remaining time
const timeLefts = {
  foo: 0,
  bar: 0,
  fizz: 0,
  buzz: 0
};
const timeLeftsUpdateInterval = setInterval(() => {
  for (const [key, val] of Object.entries(timeLefts)) {
    timeLefts[key] = Math.min(val + Math.random() * 10, 100);
  }
  if (Object.entries(timeLefts).every(([k, v]) => v >= 100)) {
    clearInterval(timeLeftsUpdateInterval);
  }
}, 1000);

// Mock $.ajax function for emulating AJAX requests
function $ajax(kwargs) {
  return {
    done: cb => {
      setTimeout(() => {
        cb(timeLefts[kwargs.data.x]);
      }, 500);
    }
  };
}

// Check for updates every second after completion of the last request
const timeLeftCheckInterval = 1000;

// Continuously monitor remaining time for an element
function getTimeLeft(el) {
  // Prepare request data
  const dataString = {
    s: "<?echo $_SESSION['currentview_'.$stamp]?>",
    r: "<?echo $search_usernumber?>",
    st: "<?echo $stamp?>",
    // Custom property for functionality
    x: el.dataset.item
  };

  // Send request to retrieve remaining time
  const req = $ajax({ // Utilizing our mock $.ajax
    type: "POST",
    url: "get_content_home.php",
    dataType: "html",
    data: dataString
  });

  // Upon completion of the request
  req.done(data => {
    // Update the element with the remaining time
    el.innerHTML = data;

    // Implement condition to cease checking for time indefinitely
    // Eventually, there won't be any time left, right? So why keep checking?
    if (data.timeleft <= 0) return;

    // Schedule the next round of time checking after a specific duration
    setTimeout(() => {
      getTimeLeft(el);
    }, timeLeftCheckInterval);
  });
}

// Initiate the process of obtaining remaining time for all .items
Array.from(document.querySelectorAll(".item"))
  .forEach(el => getTimeLeft(el));
<ul>
  <li class="item" data-item="foo"></li>
  <li class="item" data-item="bar"></li>
  <li class="item" data-item="fizz"></li>
  <li class="item" data-item="buzz"></li>
</ul>

This code resolves the issue raised in 2 Ajax non-blocking by providing each element with its logic for fetching time left and updating itself.

It also tackles the potential issue discussed in Timer in Ajax - Preemption ensuring that elements won't recheck the remaining time until the previous check completes.

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

"Implementing real-time updates for checkboxes using AJAX with PHP and

This is my modal <div class="modal fade bs-example-modal-lg" id="exampleModal<?php echo $row['t_id'] ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel<?php echo $row['t_id'] ?>"> <div class="mo ...

What is the best way to synchronize the image dimensions and source when dynamically loading images?

I am facing an issue with a function that updates images by altering the src and css (width/height) of an IMG tag within the document. Below is a simplified example to demonstrate the problem: updateImage = function(src, width, height) { $("#changeMe"). ...

Unable to connect to server using React-Native fetch. Localhost is not being used

I recently encountered an issue with my app where the fetch function used for user authentication stopped working. Despite not making any changes, the transition from React 0.27 to 0.28 seemed to have caused this problem. After scouring through numerous S ...

Contrasting ways of managing 'this' in Node.js versus the Browser

I currently have Node.js v8.10.0 installed locally. Recently, I wrote a basic script to experiment with 'this': var x = 1; var fn = function (a) { var x = a; console.log(`local x = ${x}`); console.log(`global x = ${this.x}`); } ...

Is it possible to execute this html-minifier through the terminal on Ubuntu?

I am encountering an issue while attempting to use this HTML minifier tool on Ubuntu via the command line. Every time I try to execute it, an error pops up. NodeJS and NPM installations go smoothly: root$ apt-get install -y nodejs npm Reading package lis ...

What is the best method for choosing these elements and surrounding them with a wrapper?

I need to style a title with two radio inputs by wrapping them in a form: <p><strong>Country</strong></p> <div class="radioWrapper"> <span class="label">Canada</span> <span class="radio"> ...

How can I obtain the reference to the form that initiated the Ajax.BeginForm submission?

I have a scenario where I am utilizing Ajax.BeginForm in the following manner: @using (Ajax.BeginForm("PostAction", null, new AjaxOptions { HttpMethod = "post", UpdateTargetId = "TargetID", OnComplete = "OnComplete" }, new { @class = "" })) Within the ja ...

Issue with Prestashop registration form not working properly due to AJAX bug

Currently, I am attempting to run Prestashop and have encountered an issue with registration using a custom theme. When a customer tries to register during checkout, they receive the following error message: TECHNICAL ERROR: unable to load form. Details: ...

What is the best way to synchronize the state of a single React component across various pages?

I am currently working on a React Component that includes a toggle feature (on or off) with the state being managed by the component's own state (this.state). My dilemma is ensuring that this state remains when the user navigates from one page to ano ...

What is the best approach to testing a function that relies on a reference to Firebase?

I am currently trying to write unit tests for a function within my application that interacts with Firebase, specifically by calling and updating a reference. However, I have encountered an issue while attempting to run the test - upon importing the file c ...

Tips on detecting array changes in a computed property originating from Vuex

I am currently working with an array of computed properties that are generated from the store's state: computed: { ...mapGetters([ '$tg', ]), ...mapState({ podcastList: state => state.listening.podcastList, }), tabList: ...

When we typically scroll down the page, the next section should automatically bring us back to the top of the page

When we scroll down the page, the next section should automatically bring us back to the top of the page without having to use the mouse wheel. .bg1 { background-color: #C5876F; height: 1000px; } .bg2 { background-color: #7882BB; height: 1000px; } .bg3 ...

What is the best way to tally the frequency of words in a collection of URLs using JavaScript?

I have a JSON object in WordPress that contains a list of URLs. I would like to determine the frequency of occurrence of the second part of each URL. Currently, the code snippet below is able to extract the remaining part of the URL after the prefix https ...

Middleware for connecting AJAX requests with database in Java

Current Situation: In my development of a GIS web app using Geoserver and OpenLayers, I am facing the challenge of retrieving data from the database for statistical analysis and other non-GIS related tasks. The existing library I am working with is outda ...

When using dynamic handler assignment within useEffect, the updated state is not reflected in the handler

Check out this code snippet: const App = () => { const [items, setItems] = useState<{ text: string; onClick: () => any }[]>([]) useEffect(() => setItems([ { text: 'Click me', onClick: handleButtonClick, } ...

What is the proper way to safely close the pg-promise connection in a node.js environment once all jest tests are completed?

I am facing a challenge in closing a PG-Promise database connection after running a function test in Jest. The database connection is initialized in one central location (db.js) and required in multiple places. In this scenario, it is being used by seed.j ...

Tips for locating the behavior ID within the Ajax calendar extender

Currently, I am utilizing the Ajax calendar extender to enable selection of multiple dates. <Ajax:CalendarExtender ID="CalendarExtender2" runat="Server" BehaviorID="CalendarExtender2" TargetControlID="TextBox8" PopupButtonID="Image1" OnClientD ...

Ensure at least one checkbox is selected by using jQuery validation

I wrote the code below to select multiple checkboxes and validate that at least one checkbox is selected. The data submission to the database works if I remove onsubmit="return validate_form()". However, I want to ensure that at least one checkbox is selec ...

Exploring the similarities between using jQuery AJAX post and Angular

In my current project, I have a legacy application that relies on jQuery and unfortunately cannot incorporate Angular at this time. For one specific task, I need to make an AJAX POST request to consume a web service using jQuery. Interestingly, the same ...

Discover how to easily create a clickable link for the Freshdesk API using jQuery after making an AJAX call

The code snippet below is an example of jQuery provided by Freshdesk (https://github.com/freshdesk/fresh-samples). After updating the URL and API key, it successfully functions. I am looking to modify this so that when the "Read" button is clicked, the JS ...