简体   繁体   English

防止 function 的其余部分执行,直到单击事件完成

[英]Prevent remainder of function from executing until click event is finished

Been struggling with this one for a while for a game.一直在为这个游戏苦苦挣扎。 I have an outer function that declares two variables as functions, and then puts those into a function that creates a modal.我有一个外部 function 将两个变量声明为函数,然后将它们放入创建模态的 function 中。 The user will be able to click on one of the two inner functions in the modal, but not both.用户将能够单击模态中的两个内部功能之一,但不能同时单击两者。 Once they've clicked, a new event listener is created (via the inner function they chose), and they click somewhere else on the webpage to do their action.一旦他们点击,就会创建一个新的事件监听器(通过他们选择的内部 function),然后他们点击网页上的其他地方来执行他们的操作。

The inner functions do different things but for the sake of this example I made them the same.内部函数做不同的事情,但为了这个例子,我把它们做成了一样的。

What I'm trying to achieve is for the last part of outer to not trigger until one of the event listeners of the inner function is finished.我想要实现的是,在内部 function 的事件侦听器之一完成之前, outer的最后一部分不会触发。

function inner1(a,b){

$(a).on("click", function() {

console.log("b");

}

function inner2(c,d){

$(c).on("click", function() {

console.log("d");

}



function outer(){

let x = function() {inner1(a,b)};
let y = function() {inner2(c,d)};

createModal{x,y} //creates a modal with some CSS, binding x and y to two buttons

afterEvent(); // want to trigger this AFTER createModal has created the modal AND after the user has clicked on the modal AND has clicked on the event listener.
}

outer() //--> outer executes when the user clicks on "start"

For context, the modal might offer a choice "increase money" or "increase points".对于上下文,模态可能提供“增加金钱”或“增加点数”的选择。 If they select "increase money" a new event lister is created on the "money" area of the webpage, and when they click on it the money comes up.如果他们 select “增加金钱”,则会在网页的“金钱”区域创建一个新的事件列表,当他们点击它时,金钱就会出现。 I simplified the functions for the sake of this example, but that's why the inner functions create another event listener - the user must input in order for them to run.为了这个例子,我简化了函数,但这就是内部函数创建另一个事件侦听器的原因——用户必须输入才能运行。

Additionally, the afterEvent() and inner1() and inner2() will be mixed-and-matched (hence the createModal function to build it) so I can't just put afterEvent() inside of inner1() and inner2() .此外, afterEvent()inner1()inner2()将混合匹配(因此 createModal function 来构建它)所以我不能只将afterEvent()放在inner1()inner2()内部。

I've messed around with promises, return, the frowned-upon global variable, and for loops and while loops and haven't been able to produce what i need just yet.我已经搞砸了 promises、return、皱眉头的全局变量、for 循环和 while 循环,但还不能产生我需要的东西。 Hoping someone can shed some light.希望有人可以提供一些启示。

How the user will perceive this:用户将如何看待这一点:

"ok, i clicked on start and a modal just popped up, offering me to increase my money if I click on the cow, or increase my points if I click on the pig. No matter which I choose, AFTER I've done that action, a new card will randomly be drawn for me. ok...here we go. I'll click on the first option of the modal. Hey know the cow has an outline and can be clicked, I clicked it, increased my money. and now a card was drawn for me." “好的,我点击开始,然后弹出一个模式,如果我点击牛,我会增加我的钱,如果我点击猪,我会增加我的积分。无论我选择哪个,在我完成之后动作,会随机抽一张新卡给我。好吧...这里我们go。我会点击模态的第一个选项。嘿知道牛有轮廓可以点击,我点击它,增加了我的钱。现在为我画了一张卡片。”

something like this?像这样的东西?

function inner1(a,b, fn){

$(a).on("click", function() {

console.log("b");
fn();

}

function inner2(c,d, fn){

$(c).on("click", function() {

console.log("d");
fn();

}



function outer(){

let fn = () => afterEvent(); // want to trigger this AFTER createModal has created the modal AND after the user has clicked on the modal AND has clicked on the event listener.
let x = function() {inner1(a,b, fn)};
let y = function() {inner2(c,d, fn)};

createModal{x,y} //creates a modal with some CSS, binding x and y to two buttons

}

outer() //--> outer executes when the user clicks on "start"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM