无法获得前几分钟的正确值

时间:2014-07-26 08:29:48

标签: javascript datetime web-applications momentjs

<script>
        var train_arrivals = [
        '00:10',
        '00:15',
        '00:23',
        '00:35',
        '00:43',
        '00:58',
        '01:05',
        '01:13',
        '01:20',
        '01:37',
        '01:48',
        '02:15',
        '02:25',
        '02:40',
        '02:55',
        '03:00',
        '03:09',
        '03:21',
        '03:37',
        '03:52',
        '04:15',
        '04:30',
        '04:44',
        '04:58',
        '05:16',
        '05:31',
        '05:49',
        '06:00',
        '06:26',
        '06:56',
        '08:06',
        '07:24',
        '07:43',
        '07:58',
        '08:06',
        '08:13',
        '08:28',
        '08:43',
        '08:58',
        '09:05',
        '09:13',
        '09:28',
        '09:43',
        '09:58',
        '10:05',
        '10:38',
        '11:00',
        '11:38',
        '12:38',
        '13:38',
        '14:38',
        '15:38',
        '16:00',
        '16:39',
        '17:00',
        '17:06',
        '17:27',
        '17:57',
        '20:20',
        '20:45',
        '21:50',
        '23:25',
        '23:30',
        '23:48'
        ];

        $(function() {
            var today = new Date();
            var now = moment([today.getFullYear(), today.getMonth(), today.getMinutes(), today.getHours(), today.getMinutes()]);

            var setted = '';
            var count = 1;
            for (var i=0; i<train_arrivals.length; i++){
                var entry = train_arrivals[i];

                time = entry.split(":");
                var arrival = moment([today.getFullYear(), today.getMonth(), today.getMinutes(), time[0], time[1]]);
                diff = arrival.diff(now, 'minutes');

                if (!setted && diff > 0) {
                    $("#arrival_list").append($("<li id="+count+" style='font-size: x-large' class='bg-success'>").text(diff + ' min '));
                    setted = count;
                } else
                $("#arrival_list").append($("<li id="+count+" class='bg-info'>").text(diff + ' min'));

                if (setted && count >= setted + 2) break;

                if (!setted && count > 1)
                    $('#'+count).remove();

                count++;
            }
        });
</script>

我有这个小网页应用的代码http://nexttrain.elvismdev.com/

这个想法是显示下一班火车到达火车站,因为在代码开始时列车到达时间,绿色行内的值是接近火车站的第一列火车,此后又有两列火车到达出现,并从它回来,应该显示最后一班列车在那里多少次。我无法弄清楚那里出了什么问题,因为剩下的那么长的分钟,它只会显示一个很短的时间段。

如果有人可以查看我的代码并指出我可能缺少的东西,实际上我对javascript并不那么精通,而时间计算总是让我疯狂这样的问题。

我也在使用moment.js

1 个答案:

答案 0 :(得分:1)

不是将<ul>元素添加到DOM(文档对象模型)然后再次删除它们,为什么不尝试这种方法:首先找到最近的未来出发时间,然后向后工作并在之前得到时间那。将上一次添加到DOM <ul>,然后添加最近的未来时间。像这样:

    var train_arrivals = [
    '00:10',
    '00:15',
    '00:23',
    '00:35',
    '00:43',
    '00:58',
    '01:05',
    '01:13',
    '01:20',
    '01:37',
    '01:48',
    '02:15',
    '02:25',
    '02:40',
    '02:55',
    '03:00',
    '03:09',
    '03:21',
    '03:37',
    '03:52',
    '04:15',
    '04:30',
    '04:44',
    '04:58',
    '05:16',
    '05:31',
    '05:49',
    '06:00',
    '06:26',
    '06:56',
    '08:06',
    '07:24',
    '07:43',
    '07:58',
    '08:06',
    '08:13',
    '08:28',
    '08:43',
    '08:58',
    '09:05',
    '09:13',
    '09:28',
    '09:43',
    '09:58',
    '10:05',
    '10:38',
    '11:00',
    '11:38',
    '12:38',
    '13:38',
    '14:38',
    '15:38',
    '16:00',
    '16:39',
    '17:00',
    '17:06',
    '17:27',
    '17:57',
    '20:20',
    '20:45',
    '21:50',
    '23:25',
    '23:30',
    '23:48'
    ];

    $(function() {
        var today = moment();
        var todayString = today.format("YYYY-MM-DD");
        var now = today;

        var setted = '';
        var count = 1;
        for (var i=0; i<train_arrivals.length; i++){
            var entry = train_arrivals[i];           
            var arrival = moment(todayString + ' ' + entry);
            diff = arrival.diff(now, 'minutes');

            if (!setted && diff > 0) {
                if (i > 0) {
                    var prevEntry = train_arrivals[i - 1];
                    var prevArrival = moment(todayString + ' ' + prevEntry);
                    var prevDiff = prevArrival.diff(now, 'minutes');
                    $("#arrival_list").append($("<li id="+count+" class='bg-info'>").text(prevDiff + ' min'));
                }
                $("#arrival_list").append($("<li id="+count+" style='font-size: x-large' class='bg-success'>").text(diff + ' min '));
                setted = count;
            }
            else if (diff > 0){
            $("#arrival_list").append($("<li id="+count+" class='bg-info'>").text(diff + ' min'));
            }

            if (setted && count >= setted + 2) break;
            count++;
        }
    });

这是一个有效的jsFiddle:http://jsfiddle.net/W84Gm/1/