如何在Javascript中从Date(). gettime()创建一个倒计时?

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

本文关键字:一个 倒计时 创建 gettime 中从 Javascript Date      更新时间:2023-09-26

我有一个点击函数谁从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;

在纯JavaScript中,您可以像这样使用setInterval:

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

在Ionic/Angular中,你需要将setInterval替换为包装器$interval:

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

尝试:

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