Javascript 多日期倒计时脚本

Javascript 多日期倒计时脚本,javascript,countdown,Javascript,Countdown,我知道JavaScript中有100个倒计时脚本,但我还没有找到一个可以满足我需要的脚本,所以我希望这里的专家能够提供帮助 基本上,我正在寻找的是一个脚本,它将从一个假日倒计时到下一个假日,如本例所示 感恩节还有12天11小时25分9秒 感恩节还有12天11小时25分8秒 感恩节还有12天11小时25分7秒 感恩节还有12天11小时25分6秒 感恩节还有12天11小时25分5秒 ... ... ... 直到计时器达到0,然后我希望同一个计时器显示这一点 圣诞节在:29天23小时59分59秒

我知道JavaScript中有100个倒计时脚本,但我还没有找到一个可以满足我需要的脚本,所以我希望这里的专家能够提供帮助

基本上,我正在寻找的是一个脚本,它将从一个假日倒计时到下一个假日,如本例所示


感恩节还有12天11小时25分9秒
感恩节还有12天11小时25分8秒
感恩节还有12天11小时25分7秒
感恩节还有12天11小时25分6秒
感恩节还有12天11小时25分5秒
...
...
...

直到计时器达到0,然后我希望同一个计时器显示这一点


圣诞节在:29天23小时59分59秒
圣诞节在:29天23小时59分58秒
圣诞节在:29天23小时59分57秒
圣诞节在:29天23小时59分56秒
圣诞节在:29天23小时59分55秒
...
...
...

当它达到0时,它将开始倒计时到新年或者脚本和循环中设置的下一个日期。有人知道有什么脚本可以做到这一点吗

谢谢,
Adam

这是一个起点,我不确定您需要的细节,但这一点会启动计时器,当它完成时(5秒后)启动第二个计时器(持续4秒)


函数倒计时1(计数){
如果(计数>0){
var d=document.getElementById(“countDiv”);
d、 innerHTML=“计数器1:”+计数;
setTimeout(“倒计时1”(+(count-1)+”),1000;
}
其他的
倒计时2(4);
}
函数倒计时2(计数){
如果(计数>0){
var d=document.getElementById(“countDiv”);
d、 innerHTML=“计数器2:”+计数;
setTimeout(“倒计时2”(+(count-1)+”),1000;
}
}
倒计时1(5);

这是一个起点,我不确定您需要的细节,但这一点会启动一个计时器,完成后(5秒后)会启动第二个计时器(持续4秒)


函数倒计时1(计数){
如果(计数>0){
var d=document.getElementById(“countDiv”);
d、 innerHTML=“计数器1:”+计数;
setTimeout(“倒计时1”(+(count-1)+”),1000;
}
其他的
倒计时2(4);
}
函数倒计时2(计数){
如果(计数>0){
var d=document.getElementById(“countDiv”);
d、 innerHTML=“计数器2:”+计数;
setTimeout(“倒计时2”(+(count-1)+”),1000;
}
}
倒计时1(5);

我打算建议如何修改现有的许多倒计时脚本中的一个,但几乎所有脚本都是由当前的JavaScript编码标准编写的(例如,使用字符串作为setTimeout的参数)

所以我花时间写了我自己的()。只要有信用,每个人都可以自由使用或修改它。只需将倒计时文本插入您想要的位置,并添加以下JavaScript代码,根据需要修改日期列表(示例使用五个)

/*
JavaScript的日期倒计时小部件
版权所有(c)2010 idealmachine。
为任何目的使用、复制、修改和/或分发本软件的权限
特此授予目的,无论是否收费,前提是
版权声明和本许可声明出现在所有副本中。
本软件按“原样”提供,作者不承担任何担保责任
关于本软件,包括对以下内容的所有默示保证:
适销性和适合性。在任何情况下,提交人均不对以下行为负责:
任何特殊、直接、间接或后果性损害或任何损害
因使用、数据或利润损失而导致的任何损失,无论是在
因以下原因引起的合同诉讼、疏忽或其他侵权诉讼:
或与本软件的使用或性能有关。
*/
函数开始计数(日期、元素、格式){
var now=new Date(),index=0,targetDate;
//返回发生特定月/日组合的下一个日期。
函数nextDateOccurs(arr){
var monthNotYet=now.getMonth()=targetDate){
毫秒=0;
}否则{
//找出过去几个月的最后一次
//但距离目标日期不到一个月。
而(advNow1
Thanksgiving is in: 12 Days 11 hours 25 minutes and 9 seconds
Thanksgiving is in: 12 Days 11 hours 25 minutes and 8 seconds
Thanksgiving is in: 12 Days 11 hours 25 minutes and 7 seconds
Thanksgiving is in: 12 Days 11 hours 25 minutes and 6 seconds
Thanksgiving is in: 12 Days 11 hours 25 minutes and 5 seconds
...
...
...

Christmas is in: 29 Days 23 hours 59 minutes and 59 seconds
Christmas is in: 29 Days 23 hours 59 minutes and 58 seconds
Christmas is in: 29 Days 23 hours 59 minutes and 57 seconds
Christmas is in: 29 Days 23 hours 59 minutes and 56 seconds
Christmas is in: 29 Days 23 hours 59 minutes and 55 seconds
...
...
...
<div id="countDiv"></div>

<script>
function countDown1 (count) {
  if (count > 0) {
   var d = document.getElementById("countDiv");
   d.innerHTML = "counter 1: " + count;
   setTimeout ("countDown1(" + (count-1) + ")", 1000);
   }
  else
   countDown2(4);
}
function countDown2 (count) {
  if (count > 0) {
   var d = document.getElementById("countDiv");
   d.innerHTML = "counter 2: " + count;
   setTimeout ("countDown2(" + (count-1) + ")", 1000);
   }

}
countDown1(5);
</script>
var callbacks = [
    {
        interval: 1000,
        callback: function() {
            // do Timer1 stuff
        }
    },
    {
        interval: 2000,
        callback: function() {
            // do Timer2 stuff
        }
    },
    {
        interval: 3000,
        callback: function() {
            // do Timer3 stuff
        }
    }
];

function startTimer () {
    var callback = callbacks[0];
    window.setTimeout(onTimerComplete, callback.interval);
}

function onTimerComplete () {
    var callback = callbacks.shift();
    callbacks.push(callback); // the first shall be the last
    callback.callback(); // call the callback function
    startTimer(); // set the timer for the first callback
}

startTimer();
/*
Date Countdown Widget for JavaScript
Copyright (c) 2010 idealmachine.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


function startCountdown(dates, elem, format) {
    var now = new Date(), index = 0, targetDate;

    // Returns the next date a specific month/day combination occurs.
    function nextDateOccurs(arr) {
        var monthNotYet = now.getMonth() < arr[0] - 1,
            dayNotYet = now.getMonth() == arr[0] - 1 && now.getDate() < arr[1];

        if(monthNotYet || dayNotYet) {
            // Date will pass within this calendar year
            return new Date(now.getFullYear(), arr[0] - 1, arr[1]);
        } else {
            // Date has already passed within this calendar year
            return new Date(now.getFullYear() + 1, arr[0] - 1, arr[1]);
        }
    }

    // Returns the numeric argument followed by the singular
    // or plural name of the item as is correct (and then
    // a space character).
    function formatQuantity(num, singular, plural) {
        return num + " " + (num == 1 ? singular : plural) + " ";
    }

    // Pick the target date that is closest.
    for(var j = 0; j < dates.length; ++j) {
        if(nextDateOccurs(dates[j]) < nextDateOccurs(dates[index])) {
            index = j;
        }
    }

    // Make a Date object for the target date.
    targetDate = nextDateOccurs(dates[index]);


    // Update the countdown every second.
    function updateCountdown() {
        var months = 0, millis, advNow, advNow1, words = "";

        // Update now with the current date and time.
        advNow1 = now = new Date();

        // Has the target date already passed?
        if(now >= targetDate) {
            millis = 0;
        } else {
            // Find the last time that is a whole number of months past now
            // but less than one month before the target date.
            while(advNow1 < targetDate) {
                ++months;
                advNow = advNow1;
                advNow1 = new Date(now);
                advNow1.setMonth(now.getMonth() + months);
            }
            --months;

            // Find the time difference in milliseconds within the month.
            millis = targetDate - advNow;
        }

        // Turn that into months, days, hours, minutes, and seconds.
        words += formatQuantity(months, "month", "months");
        words += formatQuantity(Math.floor(millis / 864e5), "day", "days");
        words += formatQuantity(Math.floor(millis % 864e5 / 36e5), "hour", "hours");
        words += formatQuantity(Math.floor(millis % 36e5 / 6e4), "minute", "minutes");
        words += formatQuantity(Math.floor(millis % 6e4 / 1e3), "second", "seconds");

        // Update the element.
        elem.innerHTML = format
            .replace(/%NAME%/g, dates[index][2])
            .replace(/%WORDS%/g, words);

    }

    updateCountdown();
    setInterval(updateCountdown, 1000);
}

function countdownSettings() {
    startCountdown([
            // Change the dates here to customize the script.
            [1, 1, "New Year's Day"],
            [2, 14, "Valentine's Day"],
            [7, 4, "Fourth of July"],
            [10, 31, "Halloween"],
            [12, 25, "Christmas"]

        ],
        /* Element to update */ document.getElementById("countdown"),
        /* Format of HTML inserted */ "%NAME% is in: %WORDS%"
    );
}

// Run the script only after the page has fully loaded
// to ensure that elements are accessible from the DOM.
if(window.addEventListener) {
    window.addEventListener("load", countdownSettings, false);
} else {
    window.attachEvent("onload", countdownSettings);
}
[Month, Day, Hour, Minute, "Tuesdays Meeting"]