javascript冻结选项卡和输入

javascript冻结选项卡和输入,javascript,Javascript,我正在制作一个JavaScript onkeypress函数 function report() { while (1 == 1) { window.onkeypress = function(event) { /* from this point down, keylog functions. */ // above is a variable if (event.keyCod

我正在制作一个JavaScript onkeypress函数

    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()
无论何时运行这段代码,不管它如何冻结所有形式的输入,我仍然可以滚动、打开选项卡并单击。我无法关闭选项卡、重新加载或更改JavaScript代码。我尝试过使用变量和不使用变量,并尝试过修改它。当它们只有一个键功能时,它工作得非常好,但一旦我添加了几个键,它就会冻结。 我已经削减了,削减了,但没有任何效果。
我已经检查了其他一些类似的问题,它们说要执行诸如删除变量之类的操作,我这样做了,但它仍然冻结。

它是冻结的,因为
当(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")
        }
    }

这就是所需的全部,代码将被“异步”调用它是冻结的,因为
当(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")
        }
    }

这就是所需的全部,代码将“异步”调用

您正在创建一个无限循环,该循环将冻结该选项卡

while (1 == 1) {
    //infinite loop
}
不要这样做,只需将侦听器附加到每次事件发生时触发回调的窗口:

window.addEventListener('keypress', function (e) {
    console.log(e)
});

您正在创建一个无限循环,冻结该选项卡

while (1 == 1) {
    //infinite loop
}
不要这样做,只需将侦听器附加到每次事件发生时触发回调的窗口:

window.addEventListener('keypress', function (e) {
    console.log(e)
});
尝试删除“while(1==1)”。它似乎没有留下while循环。

尝试删除“while(1==1)”。它似乎没有留下那个while循环

onkeypress属性设置并返回onkeypress事件处理程序 当前元素的代码

由于当前元素是
窗口
当您运行
报告
时,事件侦听器将侦听任何按键,因此实际上不需要
语句,而
语句实际上会冻结您的应用程序

函数报告(){
window.onkeypress=功能(事件){
如果(event.keyCode==32){
console.log(“Spacebar.\u rep”)
}
如果(event.keycode==33){
console.log(“升级标记.\u rep”)
}
如果(event.keycode==34){
console.log(“引号._rep”)
}
如果(event.keycode==35){
console.log(“hashtag.\u rep”)
}
如果(event.keycode==36){
控制台日志(“美元符号”)
}
如果(event.keycode==37){
console.log(“百分比符号_rep”)
}
如果(event.keycode==38){
console.log(“pi”)
}
}
}
报告()
1
onkeypress属性设置并返回onkeypress事件处理程序 当前元素的代码

由于当前元素是
窗口
当您运行
报告
时,事件侦听器将侦听任何按键,因此实际上不需要
语句,而
语句实际上会冻结您的应用程序

函数报告(){
window.onkeypress=功能(事件){
如果(event.keyCode==32){
console.log(“Spacebar.\u rep”)
}
如果(event.keycode==33){
console.log(“升级标记.\u rep”)
}
如果(event.keycode==34){
console.log(“引号._rep”)
}
如果(event.keycode==35){
console.log(“hashtag.\u rep”)
}
如果(event.keycode==36){
控制台日志(“美元符号”)
}
如果(event.keycode==37){
console.log(“百分比符号_rep”)
}
如果(event.keycode==38){
console.log(“pi”)
}
}
}
报告()

1
在javascript中,您不必在(1==1)
时使用
。事件侦听器将响应附加到
窗口的所有事件。使用while循环会创建无限的监听器,这会冻结应用程序。“我进行了缩进,但缩进没有任何效果。”缩进(空白)在JavaScript中基本上是不相关的。您可以在一行中编写所有内容,但它仍然可以正常工作。在javascript中,您不必使用
而(1==1)
。事件侦听器将响应附加到
窗口的所有事件。使用while循环会创建无限的监听器,这会冻结应用程序。“我进行了缩进,但缩进没有任何效果。”缩进(空白)在JavaScript中基本上是不相关的。你可以把所有的东西都写在一行中,它仍然可以工作。