代码可在控制台上运行,但不能在脚本上使用

问题描述 投票:1回答:1

我的问题是,当在特定行上设置断点时,我的代码可在控制台上运行,而在运行脚本或在任何其他行上使用断点时,则无法使用。

我有一个带有keyup侦听器的输入字段,即使输入了粘贴的输入,该字段也会触发,因此,我正在尝试开发一种解决方案,该解决方案可以识别粘贴的输入并在这样做时删除keyup侦听器。

当我在删除keyup侦听器的行上设置一个Breakpoint时,代码在控制台中运行良好,但是当我直接在页面上运行脚本或在任何其他行上使用Breakpoint时,该代码都无法正常工作。我尝试了“ DOMContentLoaded”,window.load,检查“ document.readyState”,移动了变量和函数,但仍然无济于事。

//Recognizes pasted inputs and calls to remove "Keyup" listener

searchInput.onpaste = function remove() {keyupListener();};


//Removes "Keyup" listener

function keyupListener() {
//Here's where it works when I set a Breakpoint
   searchInput.removeEventListener('keyup', dynamicPagination); 
}

// Keyup listener

searchInput.addEventListener('keyup', () => {
   dynamicPagination();
});

searchButton.addEventListener('click', () => {
   dynamicPagination();
});

代码不起作用时的行为是,它不读取removeEventListener,并使用粘贴的输入触发了keyup处理程序。

我只对香草JS解决方案感兴趣。

javascript input console addeventlistener keyup
1个回答
0
投票
//Recognizes pasted inputs and calls to remove "Keyup" listener

searchInput.onpaste = function remove() {keyupListener();};
var pasted = false;

//Removes "Keyup" listener

function keyupListener() {
    //Here's where it works when I set a Breakpoint
    pasted = true;
   //searchInput.removeEventListener('keyup', dynamicPagination); 
}

// Keyup listener

searchInput.addEventListener('keyup', () => {
    if(!pasted) dynamicPagination();
});

searchButton.addEventListener('click', () => {
   if(!pasted) dynamicPagination();
});
© www.soinside.com 2019 - 2024. All rights reserved.