這是代碼:
let chance20 = 60;
function lift20kg() {
pickNumber();
button1.addEventListener('click', function() {
chance20 =5;
commentary.textContent = `Good lift! Now you got ${chance20}% chance to lift 20kg`;
liftCommentary.textContent = 'Good Lift!';
console.log(chance20);
}))
所以問題是,第一次點擊回傳 65 - 沒問題。接下來點擊return 70和75,這樣登錄2次。然后回傳 80、85 和 90,因此一次單擊記錄 3 個數字。繼續...
我的目標當然是每次點擊將數量增加 5(將舉起更多重量的機會增加 5%)。
我想這是關于范圍界定的,但我還不夠了解它。
我會很感激你的幫助。
uj5u.com熱心網友回復:
正如康斯坦丁所提到的,如果沒有更多代碼,很難正確地知道你在做什么。但作為暗中刺殺,您可能想要完全洗掉 lift20kg 函式并僅具有事件偵聽器函式,或者洗掉事件偵聽器函式。我在下面提供了兩個示例。
第一個示例洗掉了事件偵聽器。您可能需要這個,因為您的 button1 html 標記包含 onClick="lift20kg()",在這種情況下您根本不需要事件偵聽器。
var chance20 = 60;
function lift20kg() {
pickNumber();
chance20 =5;
commentary.textContent = `Good lift! Now you got ${chance20}% chance to lift 20kg`;
liftCommentary.textContent = 'Good Lift!';
console.log(chance20);
}
或者,如果實際上不需要函式 lift20kg() ,則完全洗掉該函式并只使用這里的事件偵聽器:
var chance20 = 60;
button1.addEventListener('click', function() {
pickNumber();
chance20 =5;
commentary.textContent = `Good lift! Now you got ${chance20}% chance to lift 20kg`;
liftCommentary.textContent = 'Good Lift!';
console.log(chance20);
)}
或者,如果您確實需要兩者,則可以保留兩者,方法是將事件偵聽器從函式中拉出并使用事件偵聽器根據需要呼叫函式,如下所示:
var chance20 = 60;
function lift20kg() {
pickNumber();
chance20 =5;
commentary.textContent = `Good lift! Now you got ${chance20}% chance to lift 20kg`;
liftCommentary.textContent = 'Good Lift!';
console.log(chance20);
}
button1.addEventListener('click', function() {
lift20kg();
})
我還將“let chance20”更改為“var chance20”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/535826.html
上一篇:打字稿無法正確確定回呼函式的型別
