Search code examples
javascriptjqueryflipclock

The FlipClock .getTime() does not work? Does not update return value


I asked a similar question but did not receive. So I'll re word it. Why doesn't the flipClock function getTime() return a value for each time the clock face changes? When I output this to the console, the value is an always remains at 10. It never counts down. The clock however works perfectly fine. How do I get the current time of the clock?

var clock;

  // Instantiate a counter
  clock = new FlipClock($('.clock'), 10, {
    clockFace: 'MinuteCounter',
    autoStart: false,
    countdown: true
  });

if(clock.getTime() == 0){
   //Redirect to another page
   window.location.href = "example.html";
}else{
 console.log("This is the time now" + getTime());
}

Solution

  • because you call it once, It need to bind to interval event. create it in callback function

    var clock = new FlipClock($('.clock'), 10, {
      clockFace: 'MinuteCounter',
      autoStart: true,
      countdown: true,
      callbacks: {
        interval: function() {
          var time = this.factory.getTime().time;
          console.clear()
          console.log("Countdown now: " + time);
          if (time == 0) {
            alert('Countdown finished!');
          }
        }
      }
    });
    .clock{margin-top:30px}
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flipclock/0.7.8/flipclock.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/flipclock/0.7.8/flipclock.min.js"></script>
    
    
    <div class="clock"></div>