Try putting this in a snippet and putting a tag for the snippet in the Template(s).
I've made a couple of changes.
if (! isset($_SESSION['timestamp'])) {
$_SESSION['timestamp'] = time(); //set new timestamp
}
if(time() - $_SESSION['timestamp'] > 900) { //subtract new timestamp from the old one
echo"<script>alert('15 Minutes over!');</script>";
unset($_SESSION['username'], $_SESSION['password'], $_SESSION['timestamp']);
$_SESSION['logged_in'] = false;
$modx->sendErrorPage();
exit;
} else {
$_SESSION['timestamp'] = time(); //set new timestamp
}
I don't think it will work because using echo with JS in a snippet usually doesn't work, but it's worth a try.