简体   繁体   English

如何使用JavaScript中的Date()。getTime()创建倒数计时?

[英]How can I create a countdown from a Date().getTime() in Javascript?

I have an click function who get a number from Date().getTime(). 我有一个单击函数,它们从Date()。getTime()获得一个数字。 I want to take this number and convert him in a countdown on my view. 我想用这个数字将他转换为倒计时。

I'm using only javascript with Ionic framework 1x. 我仅在Ionic框架1x中使用javascript。

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;

In plain JavaScript you can make use of setInterval like this: 在普通的JavaScript中,您可以像这样使用setInterval

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

In Ionic/Angular, you'll need to replace setInterval with the wrapper $interval : 在Ionic / Angular中,您需要将setInterval替换为包装器$ interval

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

Try: 尝试:

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

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

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