Run PHP function after 5 min from the page done load


I want to run PHP code update a row in my database when the user did 5 min in the page. can I do this check for the time spent in PHP or I have to do it in javascript
and how can i do so.

and am getting this error every time i try to set new cookie .

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\public_html\inc\mainheader.php:7) in C:\xampp\htdocs\public_html\functions\dynamic-function.php on line 876