0

I've a problem when running this script for my JavaScript countdown (using this plugin). What it should do is take the starting time, the current time and the end time and display the remaining time.

If I set these values with normal numbers in epoch time everything works just fine, but my question is: How do I set the current time and the start to be the real current one so that the countdown will be dynamic?

I've found this line: Math.round(new Date().getTime()/1000.0);

But I don't know how to make it work, considering I'm running this script at the bottom of my HTML file, before the </html> tag.

This is the script:

<script>
$('.countdown').final_countdown({
    start: '[amount Of Time]',
    end: '[amount Of Time]',
    now: '[amount Of Time]'
});
</script>

This is how I tried to solve it, but it's not working:

//get the current time in unix timestamp seconds
var seconds = Math.round(new Date().getTime()/1000.0);
var endTime = '1388461320';

$('.countdown').final_countdown({
    start: '1362139200',
    end: endTime,
    now: seconds
});
Andreas
  • 21,535
  • 7
  • 47
  • 56
Giulio
  • 1
  • 1
  • what do you mean by dynamic? `Math.round(new Date().getTime()/1000.0)` will give you the actual time, what do you want? – Zohaib Ijaz Nov 20 '15 at 14:48
  • If I insert it at the place of [amount of time] it won't run! @ZohaibIjaz – Giulio Nov 20 '15 at 14:50
  • Look, if you need it for reference, this is the website: http://infntest.altervista.org/previousMeetings.html – Giulio Nov 20 '15 at 14:50
  • What we reall need it's the final_countdown function code – RiccardoC Nov 20 '15 at 14:53
  • Didn't I give it to you? @RiccardoC – Giulio Nov 20 '15 at 14:54
  • Are there any examples of how to use this jQuery plugin? The one I found on Google is very different. (Though, to be honest, I'm finding it hard to focus now that this song is stuck in my head...) – David Nov 20 '15 at 14:54
  • 1
    @RiccardoC https://github.com/PragmaticMates/jquery-final-countdown – Andreas Nov 20 '15 at 14:55
  • 2
    @Giulio Just check the console. The error is clearly stated `Uncaught SyntaxError: Unexpected token )` – Andreas Nov 20 '15 at 14:56
  • Already seen @RiccardoC won't work – Giulio Nov 20 '15 at 14:56
  • Alright @Andreas now the colors are showing, but time is stuck at 0.. – Giulio Nov 20 '15 at 14:59
  • Sorry Andreas. The stackoverflow "how to ask" suggest to post the code in the question and not simply post a link, because some users are behind proxy and can have some web filters that do not let them see links. If you want to add chance to recive a good reply, it's better to post importa code in the question. – RiccardoC Nov 20 '15 at 15:00
  • By the way guys the colors are showing, we need to find out why the time is stuck at 0, any ideas? – Giulio Nov 20 '15 at 15:03

1 Answers1

0

It sounds like you would like to count down from the current time to some fixed point in the future.

The following example counts down and displays the time remaining from now (whenever now might be) to some random time stamp within the next minute.

function startTimer(futureTimeStamp, display) {
  var diff;

  (function timer() {
    // how many seconds are between now and when the count down should end
    diff = (futureTimeStamp - Date.now() / 1000) | 0;

    if (diff >= 0) {
      display(diff);
      setTimeout(timer, 1000);
    }
  }());
}

// wait for the page to load.
window.onload = function() {
  var element = document.querySelector('#time'),
      now = Date.now() / 1000,
      // some random time within the next minute
      futureTimeStamp = Math.floor(now + (Math.random() * 60));

  // format the display however you wish.
  function display(diff) {
    var minutes = (diff / 60) | 0,
        seconds = (diff % 60) | 0;
    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;
    element.innerHTML = minutes + ":" + seconds;
  }
  
  startTimer(futureTimeStamp, display);
};
<span id="time"></span>

Also Math.round(new Date().getTime()/1000.0); will give you the number of seconds since the epoch, however it may be a little disingenuous to round the number. I think you would be better served by taking the floor: var timestamp = Math.floor(Date.now() / 1000)); is probably a better option.

In addition I am not sure why you need the start time, current time and end time. In order to find the remaining number of second you just need to know when the timer should end and the current time.

robbmj
  • 16,085
  • 8
  • 38
  • 63