Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
倒数计时器在JavaScript中不工作_Javascript_Jquery_Html_Css - Fatal编程技术网

倒数计时器在JavaScript中不工作

倒数计时器在JavaScript中不工作,javascript,jquery,html,css,Javascript,Jquery,Html,Css,所以我对JavaScript不是很在行,我可以在模板中生存,但不能从头开始构建任何东西。主要是,我编写网站代码,这样我就不必付钱给其他人 我有一个问题,虽然,这个倒计时计时器,我从一个模板出于某种原因不会接受一个日期倒计时“倒计时”到。它在var中所做的一切就是允许您输入null,然后倒计时到7月份的某个时候。我不知道它为什么这样做。下面是指向JsBin的链接,尽管我不经常使用它,所以我不确定我是否做对了: (函数($){ $.fn.countdown=函数(选项,回调){ //自定义“this

所以我对JavaScript不是很在行,我可以在模板中生存,但不能从头开始构建任何东西。主要是,我编写网站代码,这样我就不必付钱给其他人

我有一个问题,虽然,这个倒计时计时器,我从一个模板出于某种原因不会接受一个日期倒计时“倒计时”到。它在
var
中所做的一切就是允许您输入
null
,然后倒计时到7月份的某个时候。我不知道它为什么这样做。下面是指向JsBin的链接,尽管我不经常使用它,所以我不确定我是否做对了:

(函数($){
$.fn.countdown=函数(选项,回调){
//自定义“this”选择器
thisEl=$(本);
//自定义设置数组
变量设置={
“日期”:null,
“格式”:null,
};
//将设置数组附加到选项
如果(选项){
$.extend(设置、选项);
}
//主倒计时功能
函数倒计时\u proc(){
eventDate=Date.parse(设置['Date'])/1000;
currentDate=Math.floor($.now()/1000);
如果(eventDate=2)?天:“0”+天;
小时=(字符串(小时)。长度>=2)?小时:“0”+小时;
分钟=(字符串(分钟)。长度>=2)?分钟:“0”+分钟;
秒=(字符串(秒)。长度>=2)?秒:“0”+秒;
}
//更新倒计时的html值。
如果(!isNaN(事件日期)){
thisEl.find(“.days”).text(天);
thisEl.find(“.hours”).text(小时);
thisEl.find(“.minutes”).text(分钟);
thisEl.find(“.seconds”).text(秒);
}否则{
警报(“无效日期。以下是一个示例:2012年12月12日星期二17:30:00”);
间隔时间;
}
}
//运行函数
倒计时程序();
//循环函数
间隔=设置间隔(倒计时程序,1000);
}

}(jQuery)

您正在使用的代码在settings.Date字段中不需要日期对象。它需要一个字符串,然后将解析该字符串

此代码
eventDate=Date.parse(设置['Date'])/1000将字符串转换为日期对象,然后获取该日期的秒表示形式以与今天进行比较

因此,您可以将函数调用为
$('#elementId')。倒计时({date:'12/31/2018'})

或者,您可能希望查看一个更简单的实现,该实现不使用jQuery,并且有一个完整的工作示例和演练


编辑:将日期字符串更改为美式格式

您可以共享您的代码吗?@user7345006刚刚共享了JavaScript代码您是否检查了控制台中的错误?您添加了jQuery库了吗?@Sergiaalen“检查控制台是否有错误”是什么意思?是的,我添加了Jquery库,因为当您将日期变量定义为null时,它“起作用”……但我不能定义结束日期。我尝试过每种格式,倒计时显示的都是零。但是…当你将日期变量设置为null时,它会倒计时,只是在一个预定义的日期和时间,我似乎无法控制。我添加的代码完全替换了你说的错误行,它只是再次将所有内容重置为零。这太奇怪了…我可以看到它在JsBin中工作…但是当我复制并粘贴代码时,我仍然得到与上次相同的结果(计时器从未知时间开始倒计时),此外,这次计时器在倒计时时像是在闪烁。您能看到我的jsbin环境中可能导致问题的任何差异吗?你是否包括了我添加的最后一行调用函数的内容?这就是我要寻找的,有什么区别吗。我没有看到任何。是的,我刚刚从JsBin中获取了JS,并将其剪切粘贴到我的编辑器中。当你谈论从未知时间开始倒数的计时器时,它看起来好像是从其他地方调用的。您确定代码中没有其他调用
$(“#倒计时”).countdown(optionsObject)
?当我在没有选项对象的情况下调用函数时,会出现错误“无效日期。下面是一个示例:2012年12月12日星期二17:30:00”
(function($) {
$.fn.countdown = function(options, callback) {

    //custom 'this' selector
    thisEl = $(this);

    //array of custom settings
    var settings = { 
        'date': null,
        'format': null,
    };

    //append the settings array to options
    if(options) {
        $.extend(settings, options);
    }

    //main countdown function
    function countdown_proc() {

        eventDate = Date.parse(settings['date']) / 1000;
        currentDate = Math.floor($.now() / 1000);

        if(eventDate <= currentDate) {
            callback.call(this);
            clearInterval(interval);
        }

        seconds = eventDate - currentDate;

        days = Math.floor(seconds / (60 * 60 * 24)); //calculate the number of days
        seconds -= days * 60 * 60 * 24; //update the seconds variable with no. of days removed

        hours = Math.floor(seconds / (60 * 60));
        seconds -= hours * 60 * 60; //update the seconds variable with no. of hours removed

        minutes = Math.floor(seconds / 60);
        seconds -= minutes * 60; //update the seconds variable with no. of minutes removed

        //conditional Ss
        if (days == 1) { thisEl.find(".timeRefDays").text("day"); } else { thisEl.find(".timeRefDays").text("days"); }
        if (hours == 1) { thisEl.find(".timeRefHours").text("hour"); } else { thisEl.find(".timeRefHours").text("hours"); }
        if (minutes == 1) { thisEl.find(".timeRefMinutes").text("minute"); } else { thisEl.find(".timeRefMinutes").text("minutes"); }
        if (seconds == 1) { thisEl.find(".timeRefSeconds").text("second"); } else { thisEl.find(".timeRefSeconds").text("seconds"); }

        //logic for the two_digits ON setting
        if(settings['format'] == "on") {
            days = (String(days).length >= 2) ? days : "0" + days;
            hours = (String(hours).length >= 2) ? hours : "0" + hours;
            minutes = (String(minutes).length >= 2) ? minutes : "0" + minutes;
            seconds = (String(seconds).length >= 2) ? seconds : "0" + seconds;
        }

        //update the countdown's html values.
        if(!isNaN(eventDate)) {
            thisEl.find(".days").text(days);
            thisEl.find(".hours").text(hours);
            thisEl.find(".minutes").text(minutes);
            thisEl.find(".seconds").text(seconds);
        } else { 
            alert("Invalid date. Here's an example: 12 Tuesday 2012 17:30:00");
            clearInterval(interval); 
        }
    }

    //run the function
    countdown_proc();

    //loop the function
    interval = setInterval(countdown_proc, 1000);

}