-2

I would like to put the 1/1000 second when a button is clicked into a session variable - $_SESSION['Click'] or $_COOKIE['Click']

echo '<a href="index.php?level=done"><img src="./cat.jpg" width="'.$imgwidth.'" height="'.$imgwidth.'"></a>';

The site is called again and the new level is built. The 1/1000 second when the level is built shall be stored in another session variable $_SESSION['Begin']

Then $_SESSION['TimeNotMeasured'] += $_SESSION['Begin'] - $_SESSION['Click'] or $_COOKIE['CLick'] What would be the solution to get and store the exact time of click?

I finished the Game and it works - but I need a much better solution to measure the time between the moment when the level is finished and the milliseconds or seconds until the next level is built and ready.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
Elvenlord
  • 1
  • 3

2 Answers2

0

Easier than I thought / Made a tested and working Solution:

function myFunction() 
{
    const d = new Date();
    let time = d.getTime();
    d.setTime(time + 5000);
    let expires = "expires="+ d.toUTCString();
    document.cookie = "time="+time+";"+expires+";path=/";
    window.open('index.php?level=yes',"_top");
}
echo '<span id="tick" onclick="myFunction()"><img src="./cat_asis.jpg" width="'.$imgwidth.'" height="'.$imgwidth.'"></span>';
Elvenlord
  • 1
  • 3
-1

You're talking about things that happen in the browser:

  • Clicks.
  • Levels that finish.

You therefore have to measure these things in the browser, not in PHP. You could for instance use performance.now(), like so:

const t0 = performance.now();
doSomething();
const t1 = performance.now();
console.log(`Call to doSomething took ${t1 - t0} milliseconds.`);

After you've timed it you can send the information back to the server, with a delay and store it there.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • Well - whatever happens on the browser. There must be a way to detect the moment of a mouse click or touch on a screen and store it in a variable ... there is always a workaround The question is - which addon, which trick is the solution? – Elvenlord Feb 20 '23 at 11:15
  • @Elvenlord Yes, of course there is. I never understood, from your question, you had a problem with that. Detecting mouse clicks is easy and each click comes with a timestamp. Here is [an example of a keypress](https://developer.mozilla.org/en-US/docs/Web/API/Event/timeStamp), but the same applies to mouse clicks. Just replace "keypress" by "click". – KIKO Software Feb 20 '23 at 11:23