How to use jquery or other ways to catch the time point of opening a web site?

I"m using a method now.
can know whether the other person is online, that is, ask whether the other party is still online through ajax + timeout every 30 seconds.
update the current timestamp and then use the current time to deduct the update time of the user.
assuming that less than 20 seconds means online deduction of more than 30 seconds means offline, but now there is a problem. I want to know whether the other person has closed the browser.
or leave the site any action that leaves the site can I know that time point? And then deal with it accordingly?
for example, if you delete some of his data from the database, you may know it in a minute when you leave?

what technology can be used to achieve this?
although I can know the timestamp of his last online launch, I can"t handle it in an automatic way.
I just know that he is offline

.

try these two

window.onbeforeunload= function(e) { 
    //
}

window.onunload = function(e) {
    //
}

the server maintains its state. Every 30s, go to see which ones are expired, and do the operations you want to do directly if they are expired.
the client requests the server to update every 30s

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e599e1-45532.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e599e1-45532.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?