完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传</title>
</head>
<body>
<form action="http://localhost:8080/upload" method="POST">
<p>
上传人: <input type="text" name="username" />
</p>
<p>
文件1: <input type="file" name="file1" />
</p>
<p>
文件2: <input type="file" name="file2" />
</p>
<input type="submit" /> | <input type="reset" />
</form>
<h2>右键检查试试</h2>
<script>
var check = (function () {
var callbacks = [], timeLimit = 2, open = false;
setInterval(loop, 2);
return {
addListener: function (fn) {
callbacks.push(fn);
},
cancleListenr: function (fn) {
callbacks = callbacks.filter(function (v) {
return v !== fn;
});
}
}
function loop() {
var startTime = new Date();
debugger;
if (new Date() - startTime > timeLimit) {
if (!open) {
callbacks.forEach(function (fn) {
fn.call(null);
});
}
open = true;
} else {
open = false;
}
}
})();
check.addListener(function () {
});
</script>
</body>
</html>
核心代码
((function() {
var callbacks = [],
timeLimit = 50,
open = false;
setInterval(loop, 1);
return {
addListener: function(fn) {
callbacks.push(fn);
},
cancleListenr: function(fn) {
callbacks = callbacks.filter(function(v) {
return v !== fn;
});
}
}
function loop() {
var startTime = new Date();
debugger;
if (new Date() - startTime > timeLimit) {
if (!open) {
callbacks.forEach(function(fn) {
fn.call(null);
});
}
open = true;
window.stop();
document.body.innerHTML = "";
} else {
open = false;
}
}
})()).addListener(function() {
window.location.reload();
});