let beginTime = 0; let differTime = 0; window.onunload = function () { differTime = new Date().getTime() - beginTime; if (differTime <= 5) { console.log('浏览器关闭'); } else { console.log('浏览器刷新'); } }; window.onbeforeunload = function () { beginTime = new Date().getTime(); };