<button onclick="addLog()"> 记录日志</button>
<script>
var foo = function (){
var queue = [];
var timeInterval;
var intervalInit = 10;
var maxLogNum = 10;
function clear () {
queue.forEach(el => el())
queue.length = 0
window.clearInterval(timeInterval)
//发送日志数据
//var ajax = new XMLHttpRequest();
//ajax.open("GET", 'http://localhost:8754/api/test/ok', true)
//ajax.send(null);
}
window.onbeforeunload = clear
function keepTime () {
if (intervel == 0) {
clear()
}
console.log("interval",intervel)
intervel --;
}
return {
add: function (call) {
window.clearInterval(timeInterval)
timeInterval = setInterval(keepTime,1000)
intervel = intervalInit
queue.push(call)
queue.length > maxLogNum && clear()
},
clear
}
}
var target = foo()
var index = 0
function addLog(){
target.add(_ => {
console.log("TODO...",index ++)
})
}
</script>