2017-09-24 1 views
-1

に貼り付け:ここ、ポモドーロクロックプロジェクトをビルドてclearInterval機能にアクセスする必要があるが、これは私のコードペンリンクでカプセル化

https://codepen.io/thinkerElwin/pen/gGMvwR?editors=1010

私のコードスニペット:

$(".session").on("click", function() { 

    pause ? runSession() : (clearInterval(sessionCounting)); 

    function runSession() { 
    pause = false; 
    var start = new Date().getTime()/1000; 
    var endTime = start + parseInt(sessionLength.html() * 60); 

    function sessionTimer() { 
     time = new Date().getTime()/1000; 
     seconds = Math.ceil((endTime - time) % 60); // ceil for less than starting time 
     minutes = Math.floor((endTime - time)/60); 
     console.log(Math.ceil(endTime - time)); 
     if (minutes <= 0 && seconds == 0) { 
     clearInterval(sessionCounting); 
     alert(); 
     runBreak(); 
     } 
     if (minutes < 0) { 
     minutes = 0; 
     } 
     if (seconds < 10) { 
     seconds = '0' + seconds; 
     } 
     $("#title").html("Session is Running!"); 
     $("#timer").html(minutes + ':' + seconds); 
    }; 

    var sessionCounting = setInterval(sessionTimer, 1000); 
    sessionCounting; 

    }; 

    function runBreak() { 
    var start = new Date().getTime()/1000; 
    var endTime = start + parseInt(breakLength.html() * 60); 

    function breakTimer() { 

     time = (new Date().getTime())/1000; 
     seconds = Math.ceil((endTime - time) % 60); // ceil for less than starting time 
     minutes = Math.floor((endTime - time)/60); 
     console.log(Math.ceil(endTime - time)); 
     if (minutes <= 0 && seconds == 0) { 
     clearInterval(breakCounting); 
     alertsound(); 
     runSession(); 
     } 
     if (minutes < 0) { 
     minutes = 0; 
     } 
     if (seconds < 10) { 
     seconds = '0' + seconds; 
     } 
     $("#title").html("Time to take a break~"); 
     $("#timer").html(minutes + ':' + seconds); 
    }; 

    var breakCounting = setInterval(breakTimer, 1000); 
    breakCounting; 
    }; 
    }); 
}); 

一時停止=開始時にはtrue

ユーザーが実行中のタイマーをもう一度クリックしたときにタイマーを止める機能を追加したい、今私はこれを使用しています:

pause? runSession():(clearInterval(sessionCounting)、clearInterval(breakCounting)); 、私はこの機能にアクセスする方法を

しかし、私は

consol.log(てclearInterval(sessionCounting))

を使用しているとき、それは未定義示し、失敗しただろうか?

答えて

0

これを行う方法を見つける、非常にきれいではありません。

$(".session").on("click", function() { 

//add function that it will stop when you click again 
//clearInterval(breakCounting);clearInterval(sessionCounting); 
pause = pause == true ? false : true; 


function runSession() { 
    var start = new Date().getTime()/1000; 
    var endTime = start + parseInt(sessionLength.html() * 60); 

    function sessionTimer() { 

    if (pause == true) { 
     clearInterval(sessionCounting); 
     return; 
    }; 
    time = new Date().getTime()/1000; 
    seconds = Math.ceil((endTime - time) % 60); // ceil for less than starting time 
    minutes = Math.floor((endTime - time)/60); 
    console.log(Math.ceil(endTime - time)); 
    if (minutes <= 0 && seconds == 0) { 
     clearInterval(sessionCounting); 
     alertsound(); 
     runBreak(); 
    } 
    if (minutes < 0) { 
     minutes = 0; 
    } 
    if (seconds < 10) { 
     seconds = '0' + seconds; 
    } 
    $("#title").html("Session is Running!"); 
    $("#timer").html(minutes + ':' + seconds); 
    }; 

    var clearTimer = clearInterval(sessionCounting); 

    var sessionCounting = setInterval(sessionTimer, 1000); 
    sessionCounting; 

}; 



function runBreak() { 
    var start = new Date().getTime()/1000; 
    var endTime = start + parseInt(breakLength.html() * 60); 

    function breakTimer() { 

    if (pause == true) { 
     clearInterval(breakCounting); 
     return; 
    }; 

    time = (new Date().getTime())/1000; 
    seconds = Math.ceil((endTime - time) % 60); // ceil for less than starting time 
    minutes = Math.floor((endTime - time)/60); 
    console.log(Math.ceil(endTime - time)); 
    if (minutes <= 0 && seconds == 0) { 
     clearInterval(breakCounting); 
     alertsound(); 
     runSession(); 
    } 
    if (minutes < 0) { 
     minutes = 0; 
    } 
    if (seconds < 10) { 
     seconds = '0' + seconds; 
    } 
    $("#title").html("Time to take a break~"); 
    $("#timer").html(minutes + ':' + seconds); 
    }; 

    var breakCounting = setInterval(breakTimer, 1000); 
    breakCounting; 
}; 
runSession(); 
    }); 
}); 

コードは、私が改訂されていることスニペット:

一時停止= == trueを一時停止しますか?真偽;

コード)(IはsessionTimerに追加したスニペット:私はbreakTimerに添加

if (pause == true) { 
    clearInterval(sessionCounting); 
    return; 
}; 

コードスニペット():

if (pause == true) { 
    clearInterval(breakCounting); 
    return; 
}; 
関連する問題