简体   繁体   English

在空格键和onclick上重置计数器

[英]Reset counter on spacebar and onclick

I'm trying to make a count-down that counts down from 200 to 0 in steps of 10. 我正在尝试以10为单位从200倒数到0。

This timer can be stopped and should then be reset to 200, however, I need also the value of the moment is stopped. 该计时器可以停止,然后应重置为200,但是,我还需要停止的时间值。 The countdown fills the div #log with innerHTML . 倒数计时会用innerHTML填充div #log Whenever I "stop" the timer, I take the value of #log and place it in #price and I hide #log . 每当我“停止”的计时器,我取的价值#log并将其放置在#price和我隐藏#log The problem here is that the timer continues in the background, while I want it to reset so it can be started again by clicking on start. 这里的问题是计时器在后台继续运行,而我希望将其重置,以便可以通过单击启动再次启动它。 However, it just continues counting down and only after it's done, I can start it again. 但是,它只是继续递减计数,只有完成后,我才能再次启动它。

In the example, it doesn't take so long for it to reach 0, but in the end, it'll take 15-20 seconds to reach 0, which'll be too long to wait for. 在该示例中,到达0并不需要花费很长时间,但是最后,到达0将花费15-20秒,这太长了等待时间。

So in short: Countdown 200-0, but on click of Start -button or spacebar, it should stop the function running at the moment, so it can be started again. 简而言之:倒数200-0,但是在单击Start按钮或空格键时,它应该立即停止该函数的运行,以便可以再次启动它。

See this PEN 看到这个笔

If you have any suggestions on how to approach it completely different, you're very welcome to share! 如果您对如何完全不同有任何建议,欢迎与大家分享!

HTML 的HTML

<button id="btn" class="normal">Start</button>
<div id="log">#</div>
<div id="price"></div>

JS JS

var log = document.getElementById("log");
var btn = document.getElementById("btn");
var price = document.getElementById("price");
var counting = false;
var btnClassName = btn.getAttribute("class");

function start(count) {
  if (!counting) {
    counting = true;
    log.innerHTML = count;
    var timer = setInterval(function() {
      if (count >= 0) {
        log.innerHTML = count;
        count -= 10;
      }  else {
        clearInterval(timer);
        count = arguments[0];
        counting = false;
        btn.className = "normal";
      }
    }, 150);
  };
};

btn.onclick = function() {
  if (btnClassName == "normal") {
    start(200);
    price.style.display = 'none';
    log.style.display = 'block';
    btn.className = "counting";
    log.innerHTML = "";
  } else {
  }
};

document.body.onkeyup = function(e){
    if(e.keyCode == 32){
      price.innerHTML = log.innerHTML;
      price.style.display = 'block';
      log.style.display = 'none';
    }
}

I "re-code" your code because there are several issues there. 我“重新编码”您的代码,因为那里存在多个问题。

Just read the code and tell me if that's you are looking for or if you have any questions.. 只需阅读代码,然后告诉我您是否正在寻找或有任何疑问。

 var log = document.getElementById("log"); var btn = document.getElementById("btn"); var price = document.getElementById("price"); var counting = false; var timer; var c = 0; function start(count) { btn.blur(); if (!counting) { c = count; counting = true; log.innerHTML = count; timer = setInterval(tick, 1500); tick(); }; }; function tick() { if (c >= 0) { log.innerHTML = c; c -= 10; } else { clearInterval(timer); c = arguments[0]; counting = false; btn.className = "normal"; } } btn.onclick = function() { resetTimer(); var btnClassName = btn.getAttribute("class"); if (btnClassName == "normal") { price.style.display = 'none'; log.style.display = 'block'; btn.className = "counting"; log.innerHTML = ""; start(200); } else { pause(); } }; document.body.onkeyup = function(e) { if(e.keyCode == 32) { e.preventDefault(); pause(); } } function pause() { resetTimer(); price.innerHTML = log.innerHTML; price.style.display = 'block'; log.style.display = 'none'; btn.className = 'normal'; counting = false; } function resetTimer() { clearInterval(timer); } 
 body { font: 100% "Helvetica Neue", sans-serif; text-align: center; } /*#outer { width: 400px; height: 400px; border-radius: 100%; background: #ced899; margin: auto; } #inner { width: 350px; height: 350px; border-radius: 100%; background: #398dba; margin: auto; }*/ #log, #price { font-size: 500%; font-weight: bold; } 
 <div id="outer"> <div id="inner"> <div id="arrow"> </div> </div> </div> <button id="btn" class="normal">Start</button> <div id="log">#</div> <div id="price"></div> 

Though you have already got your answer, you can try something like this: 尽管您已经得到了答案,但是您可以尝试执行以下操作:

Also I have taken liberty to reformat your code, and for demonstration purpose, have kept delay for interval as 1000 此外,我已经采取自由重新格式化您的代码,并为示范的目的,一直保持延迟 interval1000

JSFiddle JSFiddle

 function Counter(obj) { var _initialVaue = obj.initialValue || 0; var _interval = null; var status = "Stopped"; var start = function() { this.status = "Started"; if (!_interval) { _interval = setInterval(obj.callback, obj.delay); } } var reset = function() { stop(); start(); } var stop = function() { if (_interval) { this.status = "Stopped"; window.clearInterval(_interval); _interval = null; } } return { start: start, reset: reset, stop: stop, status: status } } function init() { var counterOption = {} var count = 200; counterOption.callback = function() { if (count >= 0) { printLog(count); count -= 10; } else { counter.stop(); } }; counterOption.delay = 1000; counterOption.initialValue = 200 var counter = new Counter(counterOption); function registerEvents() { document.getElementById("btn").onclick = function() { if (counter.status === "Stopped") { count = counterOption.initialValue; counter.start(); printLog("") toggleDivs(counter.status) } }; document.onkeyup = function(e) { if (e.keyCode === 32) { printLog(counterOption.initialValue); counter.stop(); toggleDivs(counter.status) printPrice(count); } } } function printLog(str) { document.getElementById("log").innerHTML = str; } function printPrice(str) { document.getElementById("price").innerHTML = str; } function toggleDivs(status) { document.getElementById("log").className = ""; document.getElementById("price").className = ""; var hideID = (status === "Started") ? "price" : "log"; document.getElementById(hideID).className = "hide"; } registerEvents(); } init(); 
 body { font: 100% "Helvetica Neue", sans-serif; text-align: center; } .hide{ display: none; } #log, #price { font-size: 500%; font-weight: bold; } 
 <div id="outer"> <div id="inner"> <div id="arrow"> </div> </div> </div> <button id="btn" class="normal">Start</button> <div id="log">#</div> <div id="price"></div> 

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM