在javascript中设置自定义倒计时

时间:2014-09-25 05:49:19

标签: javascript jquery countdown

我的代码位于下方。我正在尝试设置倒计时,但是我无法弄清楚如何设置定时器来运行。我试过改变“var setting”。当我这样做似乎倒计时工作,但它出现故障并且数字出现但都回到“0”。真的很困惑。有人请帮忙!

(function($) {
    $.fn.countdown = function(options) {
        var settings = { 'date': "30 september 2014 16:24:00" };
        if(options) {
            $.extend(settings, options);
        }

        this_sel = $(this);

        function count_exec() {
            eventDate = Date.parse(settings['date']) / 1000; // Parse the date string
            currentDate =   Math.floor($.now() / 1000); // Find the timestamp for now
            seconds = eventDate - currentDate; // Find the number of seconds remaining
            if (seconds <= 0) { // After the event date has passed
                days = 0;
                hours = 0;
                minutes = 0;
                seconds = 0;
            } else {
                days =          Math.floor(seconds / (60 * 60 * 24));       // Divide to find the number of days remaining
                seconds -=      days * 60 * 60 * 24;                        // Subtract the number of (complete, => 24 hours) days calculated above

                hours =         Math.floor(seconds / (60 * 60));            // Get the number of hours from that modified number ^
                seconds -=      hours * 60 * 60;

                minutes =       Math.floor(seconds / 60);
                seconds -=      minutes * 60;
            }
            this_sel.find('#days').val(days).trigger('change');
            this_sel.find('#hours').val(hours).trigger('change');
            this_sel.find('#mins').val(minutes).trigger('change');
            this_sel.find('#secs').val(seconds).trigger('change');

        } // End of count_exec();

        count_exec();

        interval = setInterval(count_exec, 1000);

    } // End of the main function
}) (jQuery);

1 个答案:

答案 0 :(得分:0)

请在您的文件中输入以下代码并应用所需日期。这对我来说可以。如果有任何问题,请告诉我。

<body>
    <form name="count">
    <input type="text" size="69" name="count2">
    </form>
    <script type="text/javascript">

        //change the text below to reflect your own,
        var before = "Christmas!"
        var current = "Today is Christmas. Merry Christmas!"
        var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

        function countdown(yr, m, d) {
            theyear = yr; themonth = m; theday = d
            var today = new Date()
            var todayy = today.getYear()
            if (todayy < 1000)
                todayy += 1900
            var todaym = today.getMonth()
            var todayd = today.getDate()
            var todayh = today.getHours()
            var todaymin = today.getMinutes()
            var todaysec = today.getSeconds()
            var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec
            futurestring = montharray[m - 1] + " " + d + ", " + yr
            dd = Date.parse(futurestring) - Date.parse(todaystring)
            dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1)
            dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1)
            dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1)
            dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1)
            if (dday == 0 && dhour == 0 && dmin == 0 && dsec == 1) {
                document.forms.count.count2.value = current
                return
            }
            else
                document.forms.count.count2.value = "Only " + dday + " days, " + dhour + " hours, " + dmin + " minutes, and " + dsec + " seconds left until " + before
            setTimeout("countdown(theyear,themonth,theday)", 1000)
        }

        //enter the Future count down date using the format year/month/day
        countdown(2016, 9, 24)
    </script>
</body>