시작 시간이 다른 웹 페이지에 두 개의 타이머를 표시하려고합니다.Angular.js에 여러 타이머 기능 표시
첫 번째 타이머는 5 초 동안 만 표시하고 10 초 후에는 timer2를 표시해야합니다. 저는 Angular에 매우 익숙하며 다음 코드를 작성했습니다.
settimeout이 세 번째로 호출 될 때를 제외하고는 제대로 작동하지 않는 것 같습니다. 세 번째로 올바르게 작동하지 않으며 매우 빠르게 시작됩니다.
컨트롤러 내보기에서
// initialise variables
$scope.tickInterval = 1000; //ms
var min ='';
var sec ='';
$scope.ti = 0;
$scope.startTimer1 = function() {
$scope.ti++;
min = (Math.floor($scope.ti/60)<10)?("0" + Math.floor($scope.ti/60)):(Math.floor($scope.ti/60));
sec = $scope.ti%60<10?("0" + $scope.ti%60):($scope.ti%60);
$scope.timer1 = min + ":" + sec;
mytimeout1 = $timeout($scope.startTimer1, $scope.tickInterval); // reset the timer
}
//start timer 1
$scope.startTimer1();
$scope.$watch('timer1',function(){
if($scope.timer1 !=undefined){
if($scope.timer1 =='00:05'){
$timeout.cancel(mytimeout1);
setInterval(function(){
$scope.startTimer2()
$scope.ti = 0;
},1000)
}
}
})
//start timer 2 after 2 mins and 20 seconds
$scope.startTimer2 = function() {
$scope.ti++;
min = (Math.floor($scope.ti/60)<10)?("0" + Math.floor($scope.ti/60)):(Math.floor($scope.ti/60));
sec = $scope.ti%60<10?("0" + $scope.ti%60):($scope.ti%60);
$scope.timer2 = min + ":" + sec;
mytimeout2 = $timeout($scope.startTimer2, $scope.tickInterval);
}
$scope.$watch('timer2',function(){
if($scope.timer2 !=undefined){
if($scope.timer2 =='00:05'){
$timeout.cancel(mytimeout2);
setInterval(function(){
$scope.startTimer1();
$scope.ti = 0;
},1000)
}
}
})
을 나는 단순히 합산 그래서 당신은 기본적으로 여러가 startTimer 기능을 시작하고
<p>{{timer1}}</p>
<p>{{timer2}}</p>
당신은 setInterval을하지만, $ 간격을 사용하지 말아야 사용 : 당신은 $ 간격이 방법을 사용할 수 있습니다. – yeouuu