pennyBoy
pennyBoy

Reputation: 397

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());
}

Upvotes: 0

Views: 627

Answers (2)

ewwink
ewwink

Reputation: 19154

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>

Upvotes: 2

linguamachina
linguamachina

Reputation: 5803

Forgive me if I've misunderstood your question. If this answer is way off the mark and patronising, I apologise.

The code you provided will only execute once. Immediately after creating the clock, you call clock.getTime(). The result will be 10 at that instant. Consequently your redirect code will not execute (as the result did not equal 0), and you will immediately fall through to the code inside your else block. That code will log out the clock's current time to the console (which will still be 10 because the code executed so quickly).

In order to observe the clock counting down, you will need to repeatedly call clock.getTime(). You should read up on the concept of polling. You may find setInterval useful (see https://alligator.io/js/settimeout-setinterval/).

Upvotes: 1

Related Questions