在JavaScript中,我有一个事件监听器,我有一个单独的函数,只有在触发监听器时才需要返回一个值。
我正在寻找的例子:
document.getElementById("myBtn").addEventListener("click", function(){ alert("Hello World!"); });
function test(){
//check if myBtn has been pressed then return 1;
//otherwise keep waiting and don't return anything until myBtn is clicked.
}
我该怎么办?
答案 0 :(得分:0)
var is_button_pressed = 0;
document.getElementById("myBtn").addEventListener("click", function(){
is_button_pressed = 1;
});
function test(){
return is_button_pressed===1?1:undefined;
}