简体   繁体   English

jQuery-使用嵌套函数和setInterval()循环

[英]Jquery - looping with nested functions and setInterval()

Probably not the clearest title, but here goes - I need to display two independent countdowns on a page, accepting a user input for the starting value for each. 可能不是最清晰的标题,但是在这里-我需要在页面上显示两个独立的倒数计时,接受用户输入的每个倒计时的起始值。 When one reaches zero, the other starts and counts down to zero. 当一个达到零时,另一个启动并递减至零。 Code for this is below, and working as expected. 下面的代码可以正常工作。

I call the Timer1 function, which checks a variable for the starting value, if it exists, the count starts. 我调用Timer1函数,该函数检查变量的起始值(如果存在),则开始计数。 When the count is zero, I clear the interval, reset the display to the starting value, and fire the second timer, if it has a value assigned: 当计数为零时,我清除间隔,将显示重置为起始值,并触发第二个计时器(如果已分配值):

    function Timer1() {
        var gDuration = goTime;
        countdown = setInterval(function () {
            if (gDuration >= 0) {
                $("#durationValue").html(ToTime(gDuration));
                gDuration--;
            }
            else {
                clearInterval(countdown);
                $("#durationValue").html(ToTime(goTime));
                if (restTime != 0) {
                    Timer2();
                }
            }
        }, 1000);
    }

    function Timer2() {
        var rDuration = restTime;
        countdown = setInterval(function () {
            if (rDuration >= 0) {
                $("#restValue").html(ToTime(rDuration));
                rDuration--;
            }
            else {
                clearInterval(countdown);
                $("#restValue").html(ToTime(restTime));
            }
        }, 1000);
    }

The next step is to allow that process to run for a set number of loops - I've tried wrapping setInterval in Timer1 in a for loop, which doesn't work. 下一步是允许该进程运行一定数量的循环-我尝试将setInterval包装在Timer1中的for循环中,这是行不通的。 Any ideas how to better go about this? 任何想法如何更好地做到这一点?

for -loops don't work well with asynchronous stuff. for -loops不适用于异步内容。 Just make it a counter with an end condition as you have demonstrated with g/rDuration already. 就像已经用g/rDuration演示的那样,只需使其成为最终条件的计数器g/rDuration

With some callback abstractions, and heavy continuation-passing-style: 具有一些回调抽象和沉重的延续传递样式:

function timer(el, duration, interval, callback) {
    var countdown = setInterval(function() {
        if (duration-- >= 0) {
            el.text(ToTime(duration));
        } else {
            clearInterval(countdown);
            callback();
        }
    }, interval);
}

var goTime = …, restTime = …;
function t1(cb) {
    timer($("#durationValue"), goTime, 1000, cb);
}
function t2(cb) {
    timer($("#restValue"), restTimer, 1000, cb);
}
var loops = …;
(function loop(cb) {
    t1(function(){
        t2(function() {
            if (loop-- >= 0)
                loop(cb);
            else
                cb();
        });
    });
})(function() {
    alert("finished!");
});

The easiest thing I can think of is to have your Timer functions have a parameter with the current iteration. 我最容易想到的是让Timer函数的当前迭代具有一个参数。 Increment that value whenever one timer starts another time. 每当一个计时器开始另一时间时,递增该值。 And use that value to determine if it should indeed start the next timer. 并使用该值确定是否确实应该启动下一个计时器。

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

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