因为自动保存表单的工作方式没有标准,我会设置一个计时器setTimeout()
呃..我傻了。如果这个人试图输入他们的用户信息,当前的代码会做坏事。
未经测试,快速编写的代码:
function logMeIn() {
var el = document.getElementById("username");
if (el && el.value !== "") {
// Finish the login
} else {
window.setTimeout(logMeIn, 200);
}
}
logMeIn();
尝试2:
// This is a User Script -- runs in its own encloser, won't pollute names back to the main document.
var loginTimer;
function logMeIn() {
var el = document.getElementById("username");
if (el && el.value !== "") {
// Finish the login
} else {
loginTimer = window.setTimeout(logMeIn, 200);
}
}
logMeIn();
// can't use ".onfocus" -- its a userscript.
// Cancel the timer if the username field gets focus -- if the user tries to enter things.
document.getElementById("username").addEventListner("focus") = function(e) {
if (loginTimer) {
window.clearTimeout(loginTimer);
}
}