jquery.final-countdown.js - need help configuring

2019-08-11 15:03发布

问题:

There are instructions to configure (and how to use) this plugin here: http://www.jqueryscript.net/time-clock/Modern-Circular-jQuery-Countdown-Timer-Plugin-Final-Countdown.html

Initialize the countdown timer and set the start time, end time and current time in the javascript.

$('.countdown').final_countdown({ start: '1362139200', end: '1388461320', now: '1387461319' });

First off, I don't know what those numbers mean... it's not even explained. I deduce they mean seconds.

So I set my code this way:

    $('.countdown').final_countdown({
    'start':    0, /* ((((Jan + Feb + 3 days) * number of hours in a day) * number of minutes in an hour) * number of seconds in a minute) = total seconds */
    'end':      ((((31+28+31+2)*24)*60)*60), /* started at 9:25 pm on March 03 */
    'now':      ((((31+28+3)*24)*60)*60),
    seconds: {
        borderColor: '#8ef58e',
        borderWidth: '9'
    },
    minutes: {
        borderColor: '#ff8d72',
        borderWidth: '9'
    },
    hours: {
        borderColor: '#69ccff',
        borderWidth: '9'
    },
    days: {
        borderColor: '#ffd35c',
        borderWidth: '9'
    }
});

The problem is that every time one loads the page, it shows the same 29 days. The script is not grabbing the current time/date and comparing with another time/date in the future.

So it looks okay now (29 days), but a few days from now someone will load this page and it will be totally off.

You can see the script (and issue) here: http://www.3rd-dimension.co

I greatly appreciate any help on this.

回答1:

The problem is that your 'now' value is a fixed value (eg. 'now': ((((31+28+3)*24)*60)*60),).

Instead, you should get your 'now' value dynamically, with JavaScript's native new Date(), as shown below:

// We will get the "now" value from this variable
var today = new Date();

// My target date is this month 30th 9.25pm
var target = new Date(today);
target.setDate(30);
target.setHours(21,25,0,0);;

// Countdown start from yesterday
var yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
yesterday.setHours(0,0,0,0);;

$('.countdown').final_countdown({
    'start': yesterday.getTime() / 1000,
        'end': target.getTime() / 1000,
        'now': today.getTime() / 1000,
    seconds: {
        borderColor: '#8ef58e',
        borderWidth: '9'
    },
    minutes: {
        borderColor: '#ff8d72',
        borderWidth: '9'
    },
    hours: {
        borderColor: '#69ccff',
        borderWidth: '9'
    },
    days: {
        borderColor: '#ffd35c',
        borderWidth: '9'
    }
});

Please refer to fiddle for working example: http://jsfiddle.net/zeskysee/v0hc6cfj/11/

Hope this help :)



回答2:

<script type="text/javascript">
        $('document').ready(function () { var end = Math.floor((new Date("02/11/2018")).getTime() / 1000);
            var start = Math.floor((new Date("01/23/2018")).getTime() / 1000);
            var now = Math.floor((new Date).getTime() / 1000);
            $('.countdown').final_countdown({
                'start': start,
                'end': end,
                'now': now

            });
      });