javascript - 如何从计数器中减去 - javascript 新手
问题描述
当单击“魔术”按钮时,我需要它来触发 kashef() 函数。它必须计算两个变量并从 1000</p 中显示的计数器中减少结果。例如,如果计数器显示 1000 并且我按下“magic”btn,则计数器减少到 800(如果 resut = 200)并继续从 800 计数,无法罚款一种方法来做到这一点 - 我是 javascrip 的新手,谢谢!
let start // set on the first step to the timestamp provided
const el = document.getElementById('count') // get the element
const final = parseInt(el.textContent, 5) // parse out the final number
const duration = 100001 // duration in ms
const time;
const progress;
const step = ts => {
if (!start) {
start = ts
}
// get the time passed as a fraction of total duration
let progress = (ts - start) / duration
el.textContent = Math.floor(progress * final) * 100 // set the text
time = el.textContent
if (progress < 1) {
// if we're not 100% complete, request another animation frame
}
requestAnimationFrame(step)
const btn1 = document.getElementById('btn1');
const btn2 = document.getElementById('btn2');
const btn3 = document.getElementById('btn3');
const kashefCost;
const sovevCost;
const shatekCost;
// when buttons open
const OpentButtons = 200
if (time <= OpentButtons) {
btn1.disabled = true;
btn2.disabled = true;
btn3.disabled = true;
} else {
btn1.disabled = false;
btn2.disabled = false;
btn3.disabled = false;
}
}
function kashef() {
kashefCost = 200
const result;
result = time - kashefCost
el.value = result
}
requestAnimationFrame(step)
<html>
<header>
<p id="count">1000</p </header>
<form>
<input type="button" onclick="kashef()" id="btn3" value="(200$)magic">
<input type="button" onclick="sovev()" id="btn2" value="
(100$) roll" />
<input type="button" onclick="shatek()" id="btn1" value="(50$) mute" />
</form>
<body>
<p id="text">alert:</p>
</body>
</html>
解决方案
推荐阅读
- html - 为什么 ul 列表超出 div class="col"
- amazon-web-services - 安全组 sg-0da667222da8a6eb2 似乎不属于与输入子网相同的 VPC
- c - 用户输入被跳过
- wordpress - AWS 管道和 Github
- r - 如何在R中打印包含括号的数据框列名
- javascript - 以编程方式过滤 V8 堆快照中不需要的字符串(无需开发工具 GUI)
- javascript - 从带双引号的字符串到数组
- pytorch - “自注意力机制”和“全连接”层有什么区别?
- python - 是否可以使用 google drive api 列出 Google Drive 中指定目录中的文件?
- javascript - 使用 HTML 和 Javascript 将两个数字相加