Reputation: 61
I am making a JavaScript onkeypress function.
function report() {
while (1 == 1) {
window.onkeypress = function(event) {
/* from this point down, keylog functions. */
// above is a variable
if (event.keyCode == 32) {
console.log("Spacebar._rep")
}
if (event.keycode == 33) {
console.log("escalation-Mark._rep")
}
if (event.keycode == 34) {
console.log("quotation-Mark._rep")
}
if (event.keycode == 35) {
console.log("hashtag._rep")
}
if (event.keycode == 36) {
console.log("dollar-Sign._rep")
}
if (event.keycode == 37) {
console.log("percent-Symbol._rep")
}
if (event.keycode == 38) {
console.log("pi")
}
}
}
}
report()
whenever i run this code, however it freezes all forms of input, i can still scroll, open tabs, and click. I cannot close the tab, reload, or change the JavaScript code. I have tried it with and without variables, and i have tried modifying it. It works absolutely fine when their is only one key function, but once i add several it freezes. I have de-dented, and indented nothing has worked. I have checked out a few other similar questions, which said to do things like remove variable, and i did that and it still freezes.
Upvotes: 0
Views: 898
Reputation: 73918
The onkeypress property sets and returns the onKeyPress event handler code for the current element.
As you current element is window
when you run report
the event listener will listen to any keypress, there is no really need of a while
statement, it actually make freeze you app.
function report() {
window.onkeypress = function(event) {
if (event.keyCode == 32) {
console.log("Spacebar._rep")
}
if (event.keycode == 33) {
console.log("escalation-Mark._rep")
}
if (event.keycode == 34) {
console.log("quotation-Mark._rep")
}
if (event.keycode == 35) {
console.log("hashtag._rep")
}
if (event.keycode == 36) {
console.log("dollar-Sign._rep")
}
if (event.keycode == 37) {
console.log("percent-Symbol._rep")
}
if (event.keycode == 38) {
console.log("pi")
}
}
}
report()
1
Upvotes: 1
Reputation: 1
Try removing "while (1 == 1)". It seems like it doesn't leave that while-loop.
Upvotes: 0
Reputation: 18888
You're creating an infinite loop which freezes that tab.
while (1 == 1) {
//infinite loop
}
Instead of doing that, just attach a listener to the window that fires a callback each time the event occurs:
window.addEventListener('keypress', function (e) {
console.log(e)
});
Upvotes: 2
Reputation: 62
It is freezing because while (1==1)
is running infinitely, you should not write you code like this. It is blocking the browser
window.onkeypress = function(event) {
/* from this point down, keylog functions. */
// above is a variable
if (event.keyCode == 32) {
console.log("Spacebar._rep")
}
if (event.keycode == 33) {
console.log("escalation-Mark._rep")
}
if (event.keycode == 34) {
console.log("quotation-Mark._rep")
}
if (event.keycode == 35) {
console.log("hashtag._rep")
}
if (event.keycode == 36) {
console.log("dollar-Sign._rep")
}
if (event.keycode == 37) {
console.log("percent-Symbol._rep")
}
if (event.keycode == 38) {
console.log("pi")
}
}
this is all that is needed, the code will be "asynchronously" called
Upvotes: 0