我正试图用Javascript编写一个视频Flutter 克游戏,以此来了解它的基本知识,但我遇到了一个问题,jQuery单击事件处理程序会多次触发.

它们与下注按钮相连,在游戏中第一手下注(仅开火一次)效果良好;但是,在下注第二手牌时,它会在每次按下下注或下注按钮时触发两次点击事件(因此每次按下的正确下注金额是正确下注金额的两倍).总的来说,当按下一次下注按钮时,点击事件的触发次数遵循这种模式——序列中的ith项用于从游戏开始时下注ith手牌:1、2、4、7、11、16、22、29、37、46、,这似乎是n(n+1)/2+1,不管它值多少——我还没有聪明到能弄明白这一点,我用了OEIS.:)

这是一个带有点击事件处理程序的函数;希望这很容易理解(如果不是,请告诉我,我也想在这方面做得更好):

/** The following function keeps track of bet buttons that are pressed, until place button is pressed to place bet. **/
function pushingBetButtons() {
    $("#money").text("Money left: $" + player.money); // displays money player has left

    $(".bet").click(function() {
        var amount = 0; // holds the amount of money the player bet on this click
        if($(this).attr("id") == "bet1") { // the player just bet $1
            amount = 1;
        } else if($(this).attr("id") == "bet5") { // etc.
            amount = 5;
        } else if($(this).attr("id") == "bet25") {
            amount = 25;
        } else if($(this).attr("id") == "bet100") {
            amount = 100;
        } else if($(this).attr("id") == "bet500") {
            amount = 500;
        } else if($(this).attr("id") == "bet1000") {
            amount = 1000;
        }
        if(player.money >= amount) { // check whether the player has this much to bet
            player.bet += amount; // add what was just bet by clicking that button to the total bet on this hand
            player.money -= amount; // and, of course, subtract it from player's current pot
            $("#money").text("Money left: $" + player.money); // then redisplay what the player has left
        } else {
            alert("You don't have $" + amount + " to bet.");
        }
    });

    $("#place").click(function() {
        if(player.bet == 0) { // player didn't bet anything on this hand
            alert("Please place a bet first.");
        } else {
            $("#card_para").css("display", "block"); // now show the cards
            $(".card").bind("click", cardClicked); // and set up the event handler for the cards
            $("#bet_buttons_para").css("display", "none"); // hide the bet buttons and place bet button
            $("#redraw").css("display", "block"); // and reshow the button for redrawing the hand
            player.bet = 0; // reset the bet for betting on the next hand
            drawNewHand(); // draw the cards
        }
    });
}

请让我知道你是否有任何 idea 或建议,或者我的问题的解决方案是否与这里的另一个问题的解决方案类似(我看过许多类似标题的帖子,但没有找到一个适合我的解决方案).

推荐答案

要确保只单击一次操作,请使用以下命令:

$(".bet").unbind().click(function() {
    //Stuff
});

Javascript相关问答推荐

使用Element.children在前后移动 node

如何在react + react路由域名中使用DeliverBrowserRouter?

创建1:1比例元素,以另一个元素为中心

是否有方法在OpenWeatherMap API中获取过go 的降水数据?

仅圆角的甜甜圈图表

如何分配类型脚本中具有不同/额外参数的函数类型

我应该在redux reducer中调用其他reducer函数吗?

如何解决chrome—extension代码中的错误,它会实时覆盖google—meet的面部图像?'

如何在Javascript中的控制台上以一行形式打印循环的结果

判断表格单元格中是否存在文本框

配置WebAssembly/Emscripten本地生成问题

html + java script!需要帮助来了解为什么我得到(无效的用户名或密码)

如何迭代叔父元素的子HTML元素

Eval vs函数()返回语义

如何使本地html页面在重新加载时保持当前可隐藏部分的打开状态?

React:防止useContext重新渲染整个应用程序或在组件之间共享数据而不重新渲染所有组件

在渲染turbo流之后滚动到元素

如何在独立的Angular 应用程序中添加Lucide-Angel?

如何在Jest中模拟函数

在JavaScript中将Base64转换为JSON