使用localStorage统计页面停留时间

function statisticsStay(){
    console.log(localStorage.getItem('testSecond'));
    var second = 0;

    //开启定时器记录页面停留时间
    var timer = setInterval(function(){
        second++;
    },1000);

    //页面刷新、关闭时触发onbeforeunload事件把停留时间记录到localStorage
    window.onbeforeunload = function(){ 
        localStorage.setItem('testSecond',second);
    };
}

statisticsStay();

你可能感兴趣的:(html5,本地存储)