function function1 () {
document.addEventListener('keyup', event => {
if (event.code === 'Space') {
console.log("hello world");
})
}
function onload() {
function1();
}
function function2(){
document.addEventListener('keyup', event => {
if (event.code === 'Space') {
console.log("hello");
})
}
<body onload="onload()">
<button onclick="function2()">click</button>
</body>
This code will alert hello world when i press space key.
And when i execute the second function, i expect it to alert "hello" when i press space key
but it executes both function and alerts "hello world" and "hello" instead, so i was wondering how can i disable the function1() and only alert "hello" instead of executing the 2 functions when i execute the second function and press space key