繁体   English   中英

为什么countBreak()函数会不断更新,而不是JavaScript中的countdown()函数会不断更新?

[英]Why does the countBreak() function keep updating and not the countdown() function in JavaScript?

我希望计时器的两个功能一个接一个地运行,countdown()函数运行一次,countBreak()函数连续运行,请您解释一下为什么这样发生以及如何解决该问题,单击时如何暂停“ sessionCount div”? 这只是我所要挑战的部分代码。

 var timeCount; // to hold the timer function countdown() { var sessionData = countSession.textContent; sessionData--; document.getElementById('sessionCount').innerHTML = sessionData; //updating the content of session count timeCount = setTimeout('countdown()', 1000); //to start the timer if (sessionData < 0) { clearTimeout(timeCount); countBreak(); } } //function to countdown breaklength function countBreak() { var breakCount = breakDisplay.textContent; document.getElementById('sessionCount').innerHTML = breakCount; //updating the content of session count breakCount--; timeCount = setTimeout('countdown()', 1000); //to start the timer if (breakCount < 0) { clearTimeout(timeCount); countdown(); } } //add eventListening to the div sessionCount countSession.addEventListener('click', countdown, false); 
 <div id='displayBreak'></div> <button type='button' id='minusButton'>minus</button> <!--decreases the stored variable--> <button type='button' id='plusButton'>plus</button> <!--increses the stored variable--> <div id='displaySession'></div> <button type='button' id='minusSession'>minus</button> <!--decreases the stored variable--> <button type='button' id='plusSession'>plus</button> <!--increses the stored variable--> <div id='sessionCount'></div> 

您应该将函数定义作为setTimeout的第一个参数传递,而不是将函数调用作为字符串传递。

timeCount = setTimeout(countdown, 1000);

代替

timeCount = setTimeout('countdown()', 1000);

暂无
暂无

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

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