首页 > 解决方案 > 如何从计数器中减去 - 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>

标签: javascript

解决方案


推荐阅读