gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:06:46 25 4
gpt4 key购买 nike

<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

最佳答案

而不是添加 <ul>元素到 DOM(文档对象模型),然后再次删除它们,为什么不尝试这种方法:首先找到最近的 future 出发时间,然后向后计算并获取紧邻之前的时间。将之前的时间添加到 DOM 作为 <ul> ,然后添加最近的 future 时间。像这样的事情:

    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/

关于javascript - 无法获得前几分钟的正确值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24969112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com