Hi, I'd like to fire a javascript event at the end of a user's session

My question is two part:

1. How?

2. More importantly for me right now, how does this work? If this is a client-side event and the client is no longer there (i.e. they close their browser), how does the event get triggered?

Thank You