sending numerous ajax requests and they all seem to be returning identical results

When firing multiple ajax requests using the setinterval() function, I noticed that both requests are bringing back the same information from another page. Here is the JavaScript code:

function views()
{
    setInterval(function(){var xmllhttp
    //alert("views")
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest()
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("latestviews").innerHTML=xmlhttp.responseText
        }
    }

    xmlhttp.open("GET","latestviews.php")
    xmlhttp.send()},5000);
}

function recentposts()
{
    setInterval(function()
    {
        var xmllhttp
        //alert("recent")
        if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest()
        }
        else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
        }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("latest").innerHTML=xmlhttp.responseText
        }
    }

    xmlhttp.open("GET","recent.php")
    xmlhttp.send()},5000);
}

And here is the HTML code:

<body onload="views(),recentposts()">
    <div class="latest" id="latest">
        <span class="latestin">
            <label class="label"><i>Recent Post</i></label>
        </span>
        <?php
            $con1 = mysqli_connect('127.0.0.1','root','root','databasetry');
            $result=mysqli_query($con1,"SELECT articleid,title FROM article order by articleid desc LIMIT 6");
            $divid=0;

            while($row = mysqli_fetch_array($result))
            {   
                $id=0;  
                $id=$row['articleid'];
                echo"<div class='recent' onclick='ajaxinput($id)' id=$id style='cursor:pointer;'>";
                echo $row['title']."<br>";
                echo"</div>";
            }

            mysqli_close($con1);
        ?>

        </div>
            <div class="latestviews" id="latestviews">
            <span class="latestin">
                <label class="label"><i>Top viewed Post</i></label>
            </span>
            <?php
                $con1 = mysqli_connect('127.0.0.1','root','root','databasetry');
                $result=mysqli_query($con1,"SELECT articleid,title FROM article order by views desc LIMIT 6");
                $divid=0;

                while($row = mysqli_fetch_array($result))
                {   
                    $idd=0; 
                    $idd=$row['articleid'];
                    echo"<div class='recent' onclick='ajaxinput($idd)' id=$idd style='cursor:pointer;'>";
                    echo $row['title']."<br>";
                    echo"</div>";
                }
                mysqli_close($con1);
            ?>
        </div>
    </div>
</body>

Answer №1

To avoid browser caching of AJAX results, it is recommended to include a timestamp in the URLs.

xmlhttp.open("GET","latestviews.php" + "?time=" + new Date().getTime());

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

Encountering issues with Jquery while retrieving data from a .json URL

While attempting to retrieve data using a small script that usually works with URLs producing JSON data, I encountered a syntax error when trying it with a URL ending in .json. //error Uncaught SyntaxError: Unexpected token : http://frontier.ffxiv.com/wo ...

Choosing the perfect item with the help of a material's GridList

I've developed a react-mobx application using Material-UI, and the code structure is similar to this: render() { // defining some constants return ( <div> <img src={selectedPhoto} alt={'image title'} /> < ...

I am experiencing difficulties with my focusin not functioning properly for the input element that I added

I am working on a program that includes a dynamic table with the following structure: <table id="selectedItems"> </table> I plan to use jQuery to append elements to it like so: var i=0; $("#selectedItems").html(''); $("#sel ...

Getting Checkbox Values with Laravel and jQuery

I am facing an issue where I am only able to retrieve one value from checkboxes in jQuery, even though multiple checkboxes have different values. Here is the code snippet: <form method="post"> @foreach($services as $service) ...

Real-time Feedback: Providing live updates to users about the current connection status

My task requires pulling data from multiple datasources which can be time-consuming. To enhance user experience, I want to provide real-time information about the progress by displaying messages like "Currently retrieving data from table1" and "Now fetchin ...

Is there a way to retrieve the Response object in Express from outside the Route

Currently, my backend API is built using Express JS. Instead of using the res object directly in the route controller, I am looking to access it from a custom service. While I know that I can simply pass res as an argument to the service function and use ...

What steps should I take to enable Google Maps style on mobile devices?

Hi there! I'm having some trouble styling my Google map. Sometimes the style loads correctly in browsers, and sometimes it doesn't. Another issue I've noticed is that when I view the page on mobile platforms like Android Chrome, iOS Safari, ...

After saving any HTML, SCSS, or TS file, Angular 12 does not compile immediately

Recently I upgraded my Angular project from version 8 to 12 and then migrated to eslint. However, I have encountered an issue where the compilation does not begin immediately after saving a file. There is a delay of approximately 2 minutes before the compi ...

JS stop the timer from the previous function call before starting a new function call

Within my React app, I have implemented a slider component from Material UI. Each time the slider is moved, its onChange event triggers a function to update the state. However, I have noticed that the component rerenders for every step of the slider moveme ...

Library for visualizing JavaScript code using flowcharts and diagrams

Is there a jQuery-based javascript library available for client-side rendering and manipulation of flow-charts? I am open to other options as well. This question has been previously posed, but has not been revisited in a few years. Hopefully, there are ne ...

What is the best way to extract a certain property from a JSON string using JavaScript?

Can you guide me on how to extract the value of agent_code from this string using JavaScript? Additionally, could you please provide an explanation of the underlying logic? Abridged JSON Data: [{"name":"NYC","zone_id":"1","totalagents":"40","agents":[{ ...

What could be causing my ajax request to not be successfully compiled?

I seem to be experiencing an issue with this part of my code. Despite trying multiple methods, it still isn't functioning correctly. What steps can I take to resolve this problem? $.ajax({ url: "https://corona-api.com/countries/BR", ...

Using default value in PHP and JavaScript for a select dropdown

I have an array of cities stored in associative arrays using PHP. I am utilizing JavaScript's "on change" event to capture the value of each item. In the future, I am considering implementing geolocation for setting a default item. Here are my arrays ...

Combining Three.js with iFrame for a mix of WebGL and CSS3D

After some experimentation, I successfully created a dynamic page that seamlessly integrates WebGL and CSS3D (with valuable guidance from this helpful SO post). To add an extra touch, I included an iframe: However, I noticed that the functionality to inte ...

Leveraging both onmouseover and onmouseout for container expansion

My goal is to utilize JavaScript along with the HTML events "onmouseover" and "onmouseout" to create a dynamic container. Essentially, I want the container to act as simply a heading when the mouse is not hovering over it, but expand to display additional ...

Tips for integrating AsyncGenerators with Kotlin/JS

I am currently exploring the use of IPFS with Kotlin/JS, but my issue is not limited to that specific context. The functions ipfs.cat() and ipfs.get() return an AsyncGenerator, and I am uncertain about how to properly iterate over it using Kotlin (I am als ...

Retrieve identification details from a button within an ion-item located in an ion-list

<ion-list> <ion-item-group *ngFor="let group of groupedContacts"> <ion-item-divider color="light">{{group.letter}}</ion-item-divider> <ion-item *ngFor="let contact of group.contacts" class="contactlist" style="cl ...

What is the variance in performance between the sx prop and the makeStyles function in Material UI?

I recently started delving into Material UI and learned that it employs a CSS in JS approach to style its components. I came across 2 methods in the documentation for creating styles: First, using the sx prop: <Box sx={{ backgroundColor: 'green& ...

Adjust the color scheme of the menu to match the newly updated background image

On the homepage, there is a background slideshow playing. The issue arises when the menu becomes invisible against a dark background due to its dark font color. It would be ideal for the menu's color to dynamically change with the background. I' ...

Add up the values in the table by organizing them into groups

There is a table below that I am working with: <table> <tr> <th>Category</th> <th>Value</th> </tr> <tr> <td class="cat1">cat1</td> <td class="value" ...