Add a Time Stamp to the Ajax URL Query

I'm attempting to add a timestamp to my URL that is being called by AJAX every 5 seconds. The purpose is to prevent caching in Internet Explorer browsers. However, it seems like the AJAX call is not functioning properly now without any error messages....

The following code is functional:

<script>
  function loaddoc()
  {
   var xmlhttp=new XMLHttpRequest();
   xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
     document.getElementById("trainblock").innerHTML=xmlhttp.responseText;
    } 
   }
   xmlhttp.open("GET","networkgettrainsleicester.php",true);
   xmlhttp.send();
  }
 </script>

However, when I attempt to append the timestamp to the URL, it doesn't work:

 <script>
  function loaddoc()
  {
   var xmlhttp=new XMLHttpRequest();
   xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
     document.getElementById("trainblock").innerHTML=xmlhttp.responseText;
    } 
   }
   d=new Date();
   xmlhttp.open("GET","networkgettrainsleicester.php+'?'+d.getTime()",true);
   xmlhttp.send();
  }
 </script>

Any assistance would be greatly appreciated. Thank you

Answer №1

Make sure to add the time-stamp properly, don't just include it as a string

xmlhttp.open("GET","networkgettrainsleicester.php+'?'+d.getTime()",true);

Instead, modify it to:

xmlhttp.open("GET","networkgettrainsleicester.php?t=" + d.getTime(),true);

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

Is there a way to set the default timezone for the entire application to something like 'UTC' using JavaScript and Angular?

I'm currently developing a Hotel application where customers communicate using UTC. I have completed most of the work but everywhere I used the date object like so => new Date(). Before running the application, I need to change my local timezone to ...

What are the steps to successfully implement "Pointermove" event delegation in Safari iOS for parent/child elements?

I've encountered an issue that I'm struggling to find a solution for. My goal is to implement an event delegate using pointermove on a parent container, and I need to be able to detect when the event transitions between a child element and the pa ...

Utilize a variable function in AngularJS

Within my app.js file, I have declared functions in the following manner: var func1 = function(v1,v2,v3) { . . } var func2 = function(v1,v2,v3) { . . } Moving on to my controller.js file: var action = ""; if(..) { action = 'func1';} ...

Expanding the flexbox container to accommodate additional divs when they are added

I'm encountering an issue with a div not extending properly, causing two other divs to overlap. I've managed to position the divs correctly, but now I need the "100% all beef weenies" text to appear below the items. Any suggestions on how to achi ...

Issue with Laravel ReactJs: My changes in the ReactJs file are not being reflected on the website

I've been utilizing Reactjs within Laravel. Recently, I made some modifications to my React Component and upon refreshing my browser, the changes did not reflect. Here are the files involved: resources/views/welcome.blade.php <!doctype html&g ...

Run the document.ready function following an AJAX post request

In my custom.js file, I have multiple elements with click and other methods attached to them. All of this is enclosed within document.ready(). Everything functions as expected, but when I make an AJAX post, document.ready() doesn't get triggered again ...

Set a default value for a FormControl within react-bootstrap

When working with my email address, I utilized the <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="44362125273069262b2b30373036253404746a76706a71">[email protected]</a>. In order to specify the initial value chosen ...

Is there a way to utilize regular expressions in React to dynamically insert onclick events into specific words within a text block?

I've been experimenting with regular expressions in React to implement an onclick event for each word in a text. I've attempted two different strategies, but neither has been successful thus far. Initially, I tried: return( <div> & ...

Why isn't pagination typically positioned inside of a tbody element rather than before or after it?

I've created a user table that is based on the number parameter. I added a filter that listens to input and performs an AJAX call each time with the filter applied to the name field. However, the pagination is initially displayed ABOVE the entire ta ...

Utilizing jQuery to dynamically pass parameters to the YouTube API

When making a request to the YouTube API, I am structuring it as follows: $.get("https://www.googleapis.com/youtube/v3/channels", { part: "contentDetails", id: "somestring", key: "MY-API-KEY" } /*, ...*/ ) A hidden field contains the value id ...

When attempting to set the keyPath using a variable, an error occurs stating that the keyPath option is not a valid key path

My JSON object, stored in a variable named "jsonObjSuper", is structured like this: { "Watchlist": "My Watchlist", "Instruments": { "instrument1": [ "Company ABC", [ { "snapshotTimeUTC": "2018-11-01T00:00:00", "snapshotTime": "2018/11/ ...

Is there a way to determine if the data received from the server is in HTML or JSON format, while also locating an HTML form by name within extJS

I have a specific situation with my extjs application, where it sends an ajax request to the backend. Depending on the status of the session, the backend will respond with either json format objects for active sessions or an html page for inactive ones My ...

Refreshing website with personalized retrieved information

My goal is to populate a tab with specific data from a button click using the unique_id. However, I am encountering an issue where I want the tab to display the corresponding variable without having to refresh the page. Despite searching for solutions on ...

Define a cache validator for images generated using the imagejpeg and imagepng functions

In order to specify a cache validator for images, one can add the following lines to the .htaccess file: <IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 1 year" </IfModule> And also: <IfModule mod_headers.c&g ...

Setting up a functionality for a PHP-generated select option

When the main select tag "category" is changed, it triggers a PHP script to display a new select tag: <select id="category" onchange="showme(this);"> <option value="txt">text</option> <option value="img">image</ ...

Utilizing ViewComponent with Ajax

Does anyone know how to implement AJAX with ViewComponent in Asp.Net Core? Specifically, calling the method @Component.Invoke("ComponentName", SomeData); will display various views based on SomeData without refreshing the main view. Update Here is my app ...

Troubleshooting Ajax: What's preventing me from fetching data from an external PHP file?

I've been working on developing a grade calculator application that incorporates ajax functionality. While the ajax feature appears to be functioning smoothly, I am encountering an issue with the separate PHP file 'response.php' not receivin ...

Detect when the Keyboard is hidden in React Native

On my screen, I have a date picker and a text input. In order to prevent awkward transitions, I need to hide the keyboard before displaying the date picker. Currently, I'm resorting to a workaround because I don't know how to trigger a callback ...

Update the nested radio button to a new value

I have a series of radio button lists generated using ng-repeat. I've managed to capture the selected item successfully. Now, I am attempting to set the default value for the radio buttons. Could someone please provide assistance? Below is my code sni ...

The donut chart in Chart.js is stuck in grayscale without any colorization

I just set up a donut chart using chart.js with the following code: <div id="chartContainer" style="height: 320px; width: 320px"> <canvas id="hoursFEContainer"></canvas> </div> To use chart.js, I downlo ...