How to make a network call to an endpoint each time a page loads

Joel Anansambi

I'm running website on the local server at (http://127.0.0.1:8080).

I have a file named track located at api/track where api is simply a folder.

I have a file name main.js located at js/main.js.

Finally, I have my index.html file located at the same level with /api and /js.

I would like to make a network call to this endpoint (api/track) each time a page index.html loads.

Also, I'd like to include a timestamp ts (in milliseconds since EPOCH). An Example of the url would be /api/track?ts=1594280202864. I don't need to use the results of this call.

Here is what I did, but I'm not sure it's doing what I want especially the network call part. Can somebody help, please? Thanks

const update = () => {
    var dt = new Date();
    document.getElementById("date").innerHTML = dt;
    
    //date in millisecodn since EPOCH
    var dtInMilSec = dt.getTime();

    fetch('/api/track/dtMilSec')
        .then(response => response.json())
        .then(data => {
            console.log(data)
        });
};
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>The Date</title>
    <link href="style/main.css" rel="stylesheet" />
  </head>
  <body onload="update()">
    <h1>What's today's date?</h1>
    <p>Today is: <span id="date"></span></p>
  <script src="js/main.js"></script>
  </body>

Jim

https://stackoverflow.com/a/25984032/14649968

Maybe take a look at this answer on a related thread, and call your update() function from within the event listener for DOMContentLoaded within the main.js script, rather calling it from the onload attr in the html itself?

document.addEventListener('DOMContentLoaded', () => update(), false);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Make the page wait until it loads the second time

Each time a page loads, how do I save the Auto Increment count in a database using Hibernate?

How to call RTK query Everytime a Page Loads

How to make a script redirect only once every time an appropriate page loads?

How to make an animation trigger each time the page is scrolled?

How to make li active after page loads

Random string to generate a new image each time page loads

Nextjs: Render data from GET request each time the page loads

Make div not visible only the first time page loads

Call JQuery function each time the page is shown

How to make network loads after (or parallel to) graphics on boot

How do I make a JavaScript script run every time a specific page loads. Browser: Edge (I'm using tampermonkey)

How can I call a function when page loads with jQuery?

How do I call an API when a website page loads?

How to make this timer start itself soon as page loads

How to make BxSlider hide all slides until the page loads

How can I make this happen when page loads?

How to make background image fade in with css when page loads?

How to make Submit button stay disable when page loads

How to make my django-el-pagination page loads faster?

Flutter: How to make a page wait until data loads?

Oracle and React: How to keep order for next time page loads

How to make restTemplate.getForObject call for Mediatypes Rest endpoint

How do I make an html file include itself without refreshing the page each time a button or link is clicked?

How can make a json endpoint of a WordPress page including html markup?

How to correctly make a page for each category

how to keep calculating each time the page is refreshed

How to change page and call function at the same time?

how to iterate one time over a loop each time call a function?