函数不能实现到 while 循环中


function start() {
var work = document.getElementById("work").value;
var rest = document.getElementById("rest").value;
var rounds = document.getElementById("rounds");
var timer = document.getElementById("timer");
function countdown() {
    var roundsValue = rounds.value;
    while (roundsValue > 0) {
        var worktime = setInterval(function () {
            timer.value = work + "sec";
            work = work - 1;
            if (work === 0) {
                clearInterval(worktime);
            }
        }, 1000);
        var resttime = setInterval(function(){
            timer.value = rest + "sec";
            rest = rest-1;
            if(rest === 0){
                clearInterval(resttime);
            }
        }, 1000);  
        roundsValue = roundsValue-1;
    }
}

}

我现在正在研究我的javascript进度,我带着这个问题来到这里。我想重复与回合相同的工作时间和休息时间,但它不是这样工作的,我无法控制自己。例如:8轮工作10秒,然后休息5秒。它可能不起作用,因为函数无法实现到 WHILE 循环中。

小提琴:http://jsfiddle.net/shhyq02e/4/

这是一个快速解决方案,可能不是最好的方法,但会得到该怎么做。

http://jsfiddle.net/sahilbatla/shhyq02e/6/

function start() {
    var rounds = document.getElementById("rounds");
    var timer = document.getElementById("timer");
    var roundsValue = parseInt(rounds.value);
    (function countdown() {
        var work = document.getElementById("work").value;
        var rest = document.getElementById("rest").value;
        if (roundsValue > 0) {
            var worktime = setInterval(function () {
                timer.value = work + "sec(work)";
                work = work - 1;
                if (work === 0) {
                    clearInterval(worktime);
                    var resttime = setInterval(function() {
                      timer.value = rest + "sec(rest)";
                      rest = rest - 1;
                      if (rest === 0) {
                        clearInterval(resttime);
                        --roundsValue;
                        countdown();
                      }
                    }, 1000); 
                }
            }, 1000);
        }
    })();
}

最新更新