'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.

This timer can be stopped and should then be reset to 200, however, I need also the value of the moment is stopped. The countdown fills the div #log with innerHTML. Whenever I "stop" the timer, I take the value of #log and place it in #price and I hide #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.

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.

See this PEN

If you have any suggestions on how to approach it completely different, you're very welcome to share!

HTML

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

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';
    }
}


Solution 1:[1]

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

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!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rajesh