英文:
Why is it logging the wrong item?
问题
我试图捕获用户的按键,然后将其放入localStorage
中。但是最后的函数test()
没有正常工作。它记录的是data2
而不是data5
。在我重新加载页面并重新加载脚本后,它才会加载data5
。但我希望立即记录data5
。也许有人知道为什么!
window.onload = function () { // 检查 jQuery 是否存在
if (window.jQuery) {
abc();
} else {
script = document.createElement("script");
script.onload = abc;
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js";
document.head.appendChild(script);
}
};
function abc() { // 捕获按键事件
test = "";
test2 = "";
test3 = "";
test4 = "";
check = localStorage.getItem("test");
check2 = localStorage.getItem("test2");
check3 = localStorage.getItem("test3");
check4 = localStorage.getItem("test4");
if (check == null) { // 如果检查(保存测试值)为空,执行以下操作
data = "";
$(document).keypress(function (e) {
data += e.key;
localStorage.setItem("test", data);
});
} else if (check2 == null) { // 如果检查2(保存test2值)为空,执行以下操作
data2 = localStorage.getItem("test");
$(document).keypress(function (e) {
data2 += e.key;
localStorage.setItem("test2", data2);
});
} else if (check3 == null) { // 如果检查3(保存test3值)为空,执行以下操作
data3 = localStorage.getItem("test2");
$(document).keypress(function (e) {
data3 += e.key;
localStorage.setItem("test3", data3);
});
} else if (check4 == null) { // 如果检查4(保存test4值)为空,执行以下操作
data4 = localStorage.getItem("test3");
$(document).keypress(function (e) {
data4 += e.key;
localStorage.setItem("test4", data4);
data5 = localStorage.getItem("test4");
});
}
}
function test() { // 验证localStorage中的项目以获取上下文
data2 = localStorage.getItem("test");
data3 = localStorage.getItem("test2");
data4 = localStorage.getItem("test3");
data5 = localStorage.getItem("test4");
if (data2 || data3 || data4 || data5 != null) {
if (data2 != null && data3 && data4 && data5 == null) {
console.log(data2);
} else if (data3 != null && data4 && data5 == null) {
console.log(data3);
} else if (data4 != null && data5 == null) {
console.log(data4);
} else if (data5 != null) {
console.log(data5); // 重新加载时触发
}
} else {
return;
}
}
test();
英文:
I am trying to capture keystrokes of users, then put it in localStorage
. But the last function test()
is not working properly. It is logging data2
instead of data5
. After I reload and the script is loaded again, it is loading data5
. But I wan't to instantly log data5
. Maybe someone knows why!
window.onload = function () { // CHECK JQUERY EXISTENCE
if (window.jQuery) {
abc();
} else {
script = document.createElement("script");
script.onload = abc;
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js";
document.head.appendChild(script);
}
};
function abc() { // CAPTURE KEYSTROKES
test = "";
test2 = "";
test3 = "";
test4 = "";
check = localStorage.getItem("test");
check2 = localStorage.getItem("test2");
check3 = localStorage.getItem("test3");
check4 = localStorage.getItem("test4");
if (check == null) { // EXECUTE IF CHECK (HOLDING TEST VALUE) IS EMPTY
data = "";
$(document).keypress(function (e) {
data += e.key;
localStorage.setItem("test", data);
});
} else if (check2 == null){ // EXECUTE IF CHECK2 (HOLDING TEST2 VALUE) IS EMPTY
data2 = localStorage.getItem("test");
$(document).keypress(function (e) {
data2 += e.key;
localStorage.setItem("test2", data2);
});
} else if (check3 == null){ // EXECUTE IF CHECK3 (HOLDING TEST3 VALUE) IS EMPTY
data3 = localStorage.getItem("test2");
$(document).keypress(function (e) {
data3 += e.key;
localStorage.setItem("test3", data3);
});
} else if (check4 == null){ // EXECUTE IF CHECK4 (HOLDING TEST4 VALUE) IS EMPTY
data4 = localStorage.getItem("test3");
$(document).keypress(function (e) {
data4 += e.key;
localStorage.setItem("test4", data4);
data5 = localStorage.getItem("test4");
});
}
}
function test() { // VALIDATE lOCALSTORAGE ITEMS FOR CONTEXT
data2 = localStorage.getItem("test");
data3 = localStorage.getItem("test2");
data4 = localStorage.getItem("test3");
data5 = localStorage.getItem("test4");
if (data2 || data3 || data4 || data5 != null) {
if (data2 != null && data3 && data4 && data5 == null) {
console.log(data2);
} else if (data3 != null && data4 && data5 == null) {
console.log(data3);
} else if (data4 != null && data5 == null) {
console.log(data4);
} else if (data5 != null) {
console.log(data5); // HITS WHEN RELOADED
}
} else {
return;
}
}
test();
答案1
得分: 1
The solution is much simpler than your approach. 2 simple lines of code will do it. Upon each keystroke, append that key to your localStorage
value.
The following won't work here in Stack Overflow because of sandboxing, but a working example can be seen at this Fiddle.
Also, note that keypress
is a deprecated event and doesn't fire for non-printable characters. You should use keydown
instead.
$(document).on("keydown", function(event){
// This code will concatenate the last key pressed with a string
// of characters already present in localStorage. If nothing
// is in localStorage, then this code will create a fresh string.
let storageChars = localStorage["chars"] ? localStorage.getItem("chars") : "";
localStorage.setItem("chars", storageChars + event.key);
// Then, you can extract/examine the characters in the string in many ways,
// like looping over the string, turning the string into an array, indexing
// the string, etc.
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
英文:
The solution is much simpler than your approach. 2 simple lines of code will do it. Upon each keystroke, append that key to your localStorage
value.
The following won't work here in Stack Overflow because of sandboxing, but a working example can be seen at this Fiddle.
Also, note that keypress
is a deprecated event and doesn't fire for non-printable characters. You should use keydown
instead.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).on("keydown", function(event){
// This code will concatenate the last key pressed with a string
// of characters already present in localStorage. If nothing
// is in localStorage, then this code will create a fresh string.
let storageChars = localStorage["chars"] ? localStorage.getItem("chars") : "";
localStorage.setItem("chars", storageChars + event.key);
// Then, you can extract/examine the characters in the string in many ways,
// like looping over the string, turning the string into an array, indexing
// the string, etc.
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- end snippet -->
答案2
得分: 0
从你的其他逻辑来看,似乎你只想在data2
不为null且其他值都为null的情况下记录data2
。在这种情况下,你需要在条件语句中将每个值与null进行比较。
例如:data2 !== null && data3 === null && data4 === null && data5 === null
如果不与null进行比较,你实际上只是在测试一个"truthy"值(即不为false、0、0n、""、null、undefined或NaN的值。参见:https://developer.mozilla.org/en-US/docs/Glossary/Truthy)。
此外,看起来你可能正在尝试在这个条件语句中将每个值与null进行比较:if (data2 || data3 || data4 || data5 != null)
。如果是这样,你应该将其编码为if (data2 !== null || data3 !== null || data4 !== null || data5 !== null)
。
你可能还会发现这个链接有帮助:https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/conditionals#Logical_operators_AND_OR_and_NOT
英文:
From your other logic, it looks like you only want to log data2
if it's not null and the others are null. In that case, you want to compare each value to null in your conditional.
e.g. data2 !== null && data3 === null && data4 === null && data5 === null
If you don't compare to null, you're just testing for a "truthy" value (values that are not false, 0, 0n, "", null, undefined, or NaN. See: https://developer.mozilla.org/en-US/docs/Glossary/Truthy).
Additionally, it looks like you may be trying to compare each of the values to null in this conditional: if (data2 || data3 || data4 || data5 != null)
. If so, you'll want to code it as if (data2 !== null || data3 !== null || data4 !== null || data5 !== null).
You may also find this helpful: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/conditionals#Logical_operators_AND_OR_and_NOT
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论