移动设备上的NaN

时间:2016-10-14 15:02:11

标签: javascript nan

我有一个脚本,它会从计时器启动时开始计时一个实时计时器(从db中提取日期时间)到当前系统时间。

该脚本在桌面上完美运行 - 没有问题。但是,当我在iOS设备上浏览网站时,我得到NaN而不是时间。

这里发生了什么?

JS:

<script type="text/javascript">
        function CountUp(initDate, id){
            this.beginDate = new Date(initDate);
            this.countainer = document.getElementById(id);
            this.numOfDays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
            this.borrowed = 0, this.years = 0, this.months = 0, this.days = 0;
            this.hours = 0, this.minutes = 0, this.seconds = 0;
            this.updateNumOfDays();
            this.updateCounter();
        }

        CountUp.prototype.updateNumOfDays=function(){
            var dateNow = new Date();
            var currYear = dateNow.getFullYear();
            if ( (currYear % 4 == 0 && currYear % 100 != 0 ) || currYear % 400 == 0 ) {
                this.numOfDays[1] = 29;
            }
            var self = this;
            setTimeout(function(){self.updateNumOfDays();}, (new Date((currYear+1), 1, 2) - dateNow));
        }

        CountUp.prototype.datePartDiff=function(then, now, MAX){
            var diff = now - then - this.borrowed;
            this.borrowed = 0;
            if ( diff > -1 ) return diff;
            this.borrowed = 1;
            return (MAX + diff);
        }

        CountUp.prototype.calculate=function(){
            var currDate = new Date();
            var prevDate = this.beginDate;
            this.seconds = this.datePartDiff(prevDate.getSeconds(), currDate.getSeconds(), 60);
            this.minutes = this.datePartDiff(prevDate.getMinutes(), currDate.getMinutes(), 60);
            this.hours = this.datePartDiff(prevDate.getHours(), currDate.getHours(), 24);
            this.days = this.datePartDiff(prevDate.getDate(), currDate.getDate(), this.numOfDays[currDate.getMonth()]);
            this.months = this.datePartDiff(prevDate.getMonth(), currDate.getMonth(), 12);
            this.years = this.datePartDiff(prevDate.getFullYear(), currDate.getFullYear(),0);
        }

        CountUp.prototype.addLeadingZero=function(value){
            return value < 10 ? ("0" + value) : value;
        }

        CountUp.prototype.formatTime=function(){
            this.seconds = this.addLeadingZero(this.seconds);
            this.minutes = this.addLeadingZero(this.minutes);
            this.hours = this.addLeadingZero(this.hours);
        }

        CountUp.prototype.updateCounter=function(){
            this.calculate();
            this.formatTime();
            this.countainer.innerHTML ="" + this.hours + "" + (this.hours == 1? ":" : ":") + "" +
                "" + this.minutes + "" + (this.minutes == 1? ":" : ":") + "" +
                "" + this.seconds + "" + (this.seconds == 1? "" : "") + "";
            var self = this;
            setTimeout(function(){self.updateCounter();}, 1000);
        }

        window.onload=function(){ new CountUp('<?php echo $startTime // Start time from Database ?>', 'startTime');}

        </script>

CountUp.js:

(function($) {
    $.fn.countTo = function(options) {
        // merge the default plugin settings with the custom options
        options = $.extend({}, $.fn.countTo.defaults, options || {});

        // how many times to update the value, and how much to increment the value on each update
        var loops = Math.ceil(options.speed / options.refreshInterval),
            increment = (options.to - options.from) / loops;

        return $(this).each(function() {
            var _this = this,
                loopCount = 0,
                value = options.from,
                interval = setInterval(updateTimer, options.refreshInterval);

            function updateTimer() {
                value += increment;
                loopCount++;
                $(_this).html(value.toFixed(options.decimals));

                if (typeof(options.onUpdate) == 'function') {
                    options.onUpdate.call(_this, value);
                }

                if (loopCount >= loops) {
                    clearInterval(interval);
                    value = options.to;

                    if (typeof(options.onComplete) == 'function') {
                        options.onComplete.call(_this, value);
                    }
                }
            }
        });
    };

    $.fn.countTo.defaults = {
        from: 0,  // the number the element should start at
        to: 100,  // the number the element should end at
        speed: 1000,  // how long it should take to count between the target numbers
        refreshInterval: 100,  // how often the element should be updated
        decimals: 0,  // the number of decimal places to show
        onUpdate: null,  // callback method for every time the element is updated,
        onComplete: null,  // callback method for when the element finishes updating
    };
})(jQuery);

HTML

<div id="startTime"></div>

来自DB的DATETIME:

2016-10-13 22:10:00

1 个答案:

答案 0 :(得分:0)

我也有这个问题。这不是因为移动设备。

它在chrome中工作正常,但在safari中没有。关于ECMAScript 5 ISO-8601格式支持的mdn谈论说:

  

或者,日期/时间字符串可以是ISO 8601格式。对于   例如,&#34; 2011-10-10&#34; (仅限日期)或&#34; 2011-10-10T14:48:00&#34; (日期和   时间)可以传递和解析。

如果包含T,它将解决:

新日期(&#39; 2016-10-13T22:10:00&#39;)

您可以使用new Date('2016-10-13T22:10:00'.replace(/\s/, 'T'))