Jump to content

fetch() help and understanding


Recommended Posts

Hi, thanks for taking the time to read this. Probably pretty basic for most but i'm struggling even after watching videos and reading different forum posts. Here is what i am trying to do.

I want to watch some data without the need to refreshing the page. The database stores the data using time() and then runs through a time function to convert it to a seconds and minute output(examples "00s"/"13m 44s"). I need to pull this and have it refresh automatically and been told due to how basic this is i should be able to do it in javascript using fetch() but i am struggling to get pages talking and just completely confused how to pull the data in php and then pull using javascript.

Any advise would be greatly appreciated as well as and examples and explanations on why you are doing what you are.

Link to comment
Share on other sites

So it's to compare 2 figures simultaneously. 

Value 1 = Time

Value 2 = Time

Both times are constantly changing yes.

 

<table>
  <tr>
    <td>Name 1</td><td>Name 2</td>
  </tr>
  <tr>
    <td>Time 1</td><td>Time 2</td>
  </tr>
</table>

So the times above would be pulled from the database and updated every 2-5 seconds

Edited by Self_Taught_Still_Learning
Link to comment
Share on other sites

I'm still kinda skeptical but okay.

What you want is AJAX. fetch() can do that for you.

Make a PHP script that gets the time from the database and outputs it. Or if you're feeling up to it, put that code into some existing file where you feel it makes sense. Either way you need to end up with a URL that you can visit (in your browser) to see the time value. No HTML or anything. Just the value.

Then play around with the fetch() examples and whatever else about it you want to read. If you fetch() that URL with the time then you'll get the time.

Then you do stuff with Javascript. I don't know what because you've been pretty vague about everything.

Link to comment
Share on other sites

38 minutes ago, requinix said:

I don't understand what the database has to do with this. Is the time constantly changing? It's one value when you load the page and the potentially another value sometime later?

I've got 2 rows in a database table for 2 different accounts, each time an account changes page, the time changes in the database and is recorded. I want to display both accounts side by side and have it pull that time running through a function to convert it into time gone by but have it refresh every 2-5 seconds without needing to keep refreshing the page manually.

So on first load, user 1 and user 2 will be on 00s for example if both have just moved, then on next refresh user 2 hasn't moved but user 1 has so it would display user 1 as 00s and user 2 as 02s

Link to comment
Share on other sites

I'm honestly not sure your setup makes sense to me, but it sounds like you may want to look into web workers and forgo the database polling entirely, unless I'm misunderstanding the goal. Which is, quite honestly, very possible at this point.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.