Ad

Event Triggers Immediately After Reload. Not After "onclick"

- 1 answer

On the way to my first Pomodoro-App (timer) shipwrecking with oncLick, which should start the counter… It starts even without beeing triggered by button click. How can I prevent that?

let display = document.getElementById("counter");

document.getElementById("ticker").onclick = function() {
  timeChange(25);
};

// document.getElementById("ticker").addEventListener("click", function() {
//   timeChange(25);
// });

function timeChange(seconds) {
  // let seconds = 25;
  seconds -= 1;
  display.innerText = seconds;
  page.firstChild.nodeValue = seconds;
  if (seconds > 0) {
    setTimeout(timeChange, 1000, seconds);
  }
}
timeChange(25);
<body>
  <div id="page">
    <div id="counter"></div>
    <button id="ticker">Start Pomodoro</button>
  </div>
</body>

I already found an answer to the same(?) issue:

That's because you're assigning the result of executing rollDice(results) to your clickButton.onclick function

But I did not(?) assign the result of my function to onclick

Is it because of the argument (25) I put into the function to which the onclick is assigned to?

Many thanks in advance

Ad

Answer

Remove: timeChange(25); from the bottom of your code

Ad
source: stackoverflow.com
Ad