我希望能够单击一个 id 为 pause 的元素来开始对时间对象中的元素进行计数,如果我再次单击暂停,它将停止它并重新单击启动它,就像中的切换功能一样JQuery 但具有 setInteval 函数,我将如何执行此操作?
$("#pause").click(function(ffe) {
if(on == true) {
on = false
alert("on");
}
else {
on = true;
alert("off");
}
if(on == false) {
setInterval(function() {
$("#timet ul").append("<li>" + $("#time ul")
.children('li').length +"</li>");
}, 100);
}
else {
alert("Error");
}
});
最佳答案
一种经典技术是使用单个主 setInterval 循环并简单地使用 if..else 逻辑来确定需要运行的内容。许多 JavaScript 游戏都是这样工作的:
var on = true;
// Our master scheduler:
setInterval(function() {
if (on) {
$("#timet ul").append("<li>" + $("#time ul")
.children('li').length +"</li>");
}
}, 100);
// Code to handle the pause button
$("#pause").click(function(ffe) {
on = !on;
}
关于javascript - 我将如何在 jQuery 中切换 setInterval 函数的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11857205/