如何在页面上记录访问持续时间?

How to log visit duration on page?

Currently im logging page hits only, by inserting IP, user agent etc. into database.

I know, that I have to use javascript and "onbeforeunload", but I don't know what to do next.

Difficult to test it through jsFiddle (it seems), but you can do something like:

function enter() {
    this.crono = new Date().getMilliseconds();
}

function leave() {
    this.crono = new Date().getMilliseconds() - this.crono;
    alert(this.crono);
}

window.onload = enter;
window.onbeforeunload = leave;

In place of that alert(this.crono); you could launch an ajax call to the server with the crono value.