Search code examples
javascriptdateionic-frameworktimercountdown

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


I have an click function who get a number from 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.

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;

Solution

  • In plain JavaScript you can make use of setInterval like this:

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

    In Ionic/Angular, you'll need to replace setInterval with the wrapper $interval:

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