Event triggers immediately after reload. Not after “onclick” [closed]

淺唱寂寞╮ 提交于 2019-12-13 08:20:51

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/54840493/event-triggers-immediately-after-reload-not-after-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!