2016-11-07 20 views
0

我有一個從Date()。getTime()獲取數字的單擊函數。我想採取這個數字,並在我的觀點倒計時他。如何從JavaScript中的Date()。getTime()中創建倒計時?

我只使用JavaScript與Ionic框架1x。

var currentDate = new Date().getTime(); 
console.log(currentDate); 

var secondDate = new Date(item.date).getTime(); 
console.log(secondDate); 
secondDate = new Date(secondDate).getSeconds(); 
console.log(secondDate); 
$scope.countdown = secondDate; 

回答

1

在普通的JavaScript,你可以使用setInterval這樣的:

setInterval(function() { 
 
    console.log(new Date().getTime()); 
 
}, 1000);

離子/角,你需要與包裝$interval更換setInterval

$interval(function() { 
    $scope.countdown = new Date().getTime(); 
}, 1000); 
+0

謝謝czosel!它的工作完美。大約在那個時候,有一個簡單的方法來倒轉倒計時? –

+0

不客氣!要倒轉倒計時,您可以計算當前日期和倒計時日期之間的差異,並將該值與範圍綁定。 –

0

Try:

var interval = setInterval(function() { 
$scope.countdown -= 1; 
    if ($scope.countdown == 0) { 
     clearInterval(interval); 
    } 
},1000);