JavaScript doesn’t update the footer on first click.

huangapple go评论110阅读模式
英文:

The button to update the 'hp' variable in my website works internally, but for some reason the javascript doesn't update the footer on first click

问题

以下是代码的翻译部分:

app.py:

@app.route("/dealers", methods=["GET", "POST"])
@login_required
def dealers():
    if request.method == "POST":
        id = int(request.form['user_id'])

        # 使用用户数据初始化行
        rows = db.execute("SELECT * FROM users WHERE id = ?", session["user_id"])

        # 确保用户有足够的hp
        if rows[0]["hp"] < 1:
            return jsonify({'success': False})

        # 减少用户的hp并增加dealer的hp
        db.execute("UPDATE users SET hp = hp - 1, lucky = lucky + 1 WHERE id = ?", session["user_id"])
        db.execute("UPDATE users SET hp = hp + 1 WHERE id = ?", id)

        session["hp"] = rows[0]["hp"]
        hp = session["hp"]

        return jsonify({'success': True, 'hp': hp})
    else:
        return render_template("dealers.html")

@app.route("/players", methods=["GET", "POST"])
@login_required
def players():
    if request.method == "POST":
        id = int(request.form['user_id'])

        # 使用用户数据初始化行
        rows = db.execute("SELECT * FROM users WHERE id = ?", session["user_id"])

        # 减少用户的hp并增加dealer的hp
        db.execute("UPDATE users SET hp = hp - 10, lucky = lucky + 10 WHERE id = ?", session["user_id"])
        db.execute("UPDATE users SET bounty = bounty + 1 WHERE id = ?", id)

        session["hp"] = rows[0]["hp"]
        hp = session["hp"]

        return jsonify({'success': True, 'hp': hp})
    else:
        players = db.execute("SELECT * FROM users ORDER BY hp")
        # 添加偏移值以跳过前几个用户

        return render_template("players.html", players=players)

script.js:

function tip(user_id) {
    $.ajax({
        type: 'POST',
        url: '/dealers',
        data: {'user_id': user_id},
        success: function(data) {
            if (data.success) {
                $("#dialog-message").dialog({
                    modal: true,
                    buttons: {
                        Ok: function() {
                            $(this).dialog("close");
                        }
                    }
                });
                $("#dialog-message").text("您已成功打赏");
                setTimeout(function() {
                    $("#dialog-message").dialog("close");
                }, 4000); // 5秒后关闭对话框

                // 更新#value-container元素中的HP值
                if (data.hp !== undefined) {
                    $('#value-container').text("HP: " + data.hp);
                }
            } 
        }
    });
}

function placeBounty(user_id) {
    $.ajax({
        type: 'POST',
        url: '/players',
        data: {'user_id': user_id},
        success: function(data) {
            if (data.success) {
                $("#dialog-message").dialog({
                    modal: true,
                    buttons: {
                        Ok: function() {
                            $(this).dialog("close");
                        }
                    }
                });
                $("#dialog-message").text("您已成功设置赏金!");
                setTimeout(function() {
                    $("#dialog-message").dialog("close");
                }, 4000); // 5秒后关闭对话框 
                $('#value-container').html("HP: " + data.hp);
            } 
        }
    });
}

layout.html:

<footer>
    <div id="value-container"> HP: {{ session["hp"] }} </div>
</footer>
英文:

The variables are updated internally for every click of the button, as it should, but the footer only starts updating after the first click, and when I switch to the players page to place a "bounty", the first click ends up decreasing the footer value by 1 (which should be the tipping function) and only decreasing the hp value by 10 after the first click. What might be the cause of this?

app.py:

@app.route(&quot;/dealers&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])
@login_required
def dealers():
if request.method == &quot;POST&quot;:
id = int(request.form[&#39;user_id&#39;])
#initialize rows with user data
rows = db.execute(&quot;SELECT * FROM users WHERE id = ?&quot;, session[&quot;user_id&quot;])
# making sure user has enough hp
if rows[0][&quot;hp&quot;] &lt; 1:
return jsonify({&#39;success&#39;: False})
# minus users hp and add dealers hp
db.execute(&quot;UPDATE users SET hp = hp - 1, lucky = lucky + 1 WHERE id = ?&quot;, session[&quot;user_id&quot;])
db.execute(&quot;UPDATE users SET hp = hp + 1 WHERE id = ?&quot;, id)
session[&quot;hp&quot;] = rows[0][&quot;hp&quot;]
hp = session[&quot;hp&quot;]
return jsonify({&#39;success&#39;: True, &#39;hp&#39;: hp})
else:
return render_template(&quot;dealers.html&quot;)
@app.route(&quot;/players&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])
@login_required
def players():
if request.method == &quot;POST&quot;:
id = int(request.form[&#39;user_id&#39;])
#initialize rows with user data
rows = db.execute(&quot;SELECT * FROM users WHERE id = ?&quot;, session[&quot;user_id&quot;])
# minus users hp and add dealers hp
db.execute(&quot;UPDATE users SET hp = hp - 10, lucky = lucky + 10 WHERE id = ?&quot;, session[&quot;user_id&quot;])
db.execute(&quot;UPDATE users SET bounty = bounty + 1 WHERE id = ?&quot;, id)
session[&quot;hp&quot;] = rows[0][&quot;hp&quot;]
hp = session[&quot;hp&quot;]
return jsonify({&#39;success&#39;: True, &#39;hp&#39;: hp})
else:
players = db.execute(&quot;SELECT * FROM users ORDER BY hp&quot;)
# add OFFSET int for skipping first few users^
return render_template(&quot;players.html&quot;, players=players)

script.js:

function tip(user_id) {
$.ajax({
type: &#39;POST&#39;,
url: &#39;/dealers&#39;,
data: {&#39;user_id&#39;: user_id},
success: function(data) {
if (data.success) {
$(&quot;#dialog-message&quot;).dialog({
modal: true,
buttons: {
Ok: function() {
$(this).dialog(&quot;close&quot;);
}
}
});
$(&quot;#dialog-message&quot;).text(&quot;You have successfully tipped&quot;);
setTimeout(function() {
$(&quot;#dialog-message&quot;).dialog(&quot;close&quot;);
}, 4000); // close dialog after 5 seconds
// update HP value in #value-container element
if (data.hp !== undefined) {
$(&#39;#value-container&#39;).text(&quot;HP: &quot; + data.hp);
}
} 
});
}
function placeBounty(user_id) {
$.ajax({
type: &#39;POST&#39;,
url: &#39;/players&#39;,
data: {&#39;user_id&#39;: user_id},
success: function(data) {
if (data.success) {
$(&quot;#dialog-message&quot;).dialog({
modal: true,
buttons: {
Ok: function() {
$(this).dialog(&quot;close&quot;);
}
}
});
$(&quot;#dialog-message&quot;).text(&quot;You have successfully placed a bounty!&quot;);
setTimeout(function() {
$(&quot;#dialog-message&quot;).dialog(&quot;close&quot;);
}, 4000); // close dialog after 5 seconds 
$(&#39;#value-container&#39;).html(&quot;HP: &quot; + data.hp);
} 

layout.html:


//some code
&lt;footer&gt;
&lt;div id=&quot;value-container&quot;&gt; HP: {{ session[&quot;hp&quot;] }} &lt;/div&gt;
&lt;/footer&gt;

答案1

得分: 0

更新:
所以显然在我的app.py文件中,我忘记在更新hp变量之后重新更新具有用户数据的行,这就是为什么第一次单击按钮时会有延迟。

英文:

Update:
So apparently in my app.py file, I forgot to re-update rows with the user data AFTER updating the hp variable, which was why there is a delay when you first click the button.

huangapple
  • 本文由 发表于 2023年3月12日 07:51:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定