为什么在JavaScript中countBreak()函数一直在更新,而不是countdown()函数
Why does the countBreak() function keep updating and not the countdown() function in JavaScript?
我希望计时器的两个函数一个接一个地运行,countdown()函数运行一次,countBreak()函数持续运行,你能解释一下为什么会这样,如何解决这个问题,以及如何在单击时暂停"sessionCountdiv"吗?这只是我面临挑战的代码部分。
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);
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- JQuery合并了keyup和focusout两个函数
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 我可以在json对象中添加一个函数吗
- AngularJS:我可以跳过函数参数回调吗
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- jquery点击函数select&取消选择
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- Node.js v6.2.0类扩展不是函数错误
- 比较从函数和生成的日期对象
- jQuery中是否内置了任何字符串格式化函数
- 为什么在JavaScript中countBreak()函数一直在更新,而不是countdown()函数