在json
文件中有这种消息(在“时间”日期,以毫秒为单位)
$.getJSON('data/messages.json', callback);
callback([{
"time": "1499204237",
"user": "Max",
"message": "Hello"
}, {
"time": "1499204238",
"user": "Mike",
"message": "Hi"
}]);
function callback(respond) {
for (var i = 0; i < respond.length; i++) {
var data = respond[i];
var now = new Date();
var time = (data.time);
for(var j = 0; j < time.length; j++){
var time_diff = time[j] - now;
while(time_diff < 3600){
var date = new Date(+time * 1000);
var new_date = new Date(date);
var res = [new_date.getHours(), new_date.getMinutes(), new_date.getSeconds()].map(function (x) {
return x < 10 ? "0" + x : x;
}).join(":");
var rowClone = $('.mess_hide').clone().removeClass('mess_hide');
$('#messages').append(rowClone);
$('.time', rowClone).html(res);
$('.name', rowClone).html(data.user);
$('.message', rowClone).html(data.message);
$('.scroller').scrollTop($('#messages').height());
}
}
}
}
.scroller {
width: 490px;
height: 255px;
max-height: 255px;
overflow-y: auto;
overflow-x: hidden;
}
table#messages {
min-height: 260px;
width: 100%;
background: #fffecd;
border: none;
}
table#messages::-webkit-scrollbar {
width: 1em;
}
table#messages::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
}
table#messages::-webkit-scrollbar-thumb {
background-color: darkgrey;
outline: 1px solid slategrey;
}
tr {
height: 20%;
display: block;
}
td.time,
td.name {
width: 70px;
max-width: 75px;
text-align: center;
}
td.name {
font-weight: bold;
}
form#text_submit {
display: inline-flex;
align-items: flex-start;
}
input#text {
width: 370px;
height: 30px;
margin-top: 20px;
background: #fffecd;
font-family: 'Montserrat';
font-size: 16px;
border: none;
align-self: flex-start;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="scroller">
<table id="messages">
<tr class="mess_hide">
<td class="time"></td>
<td class="name"></td>
<td class="message"></td>
</tr>
</table>
</div>
<form method="POST" id="easyForm">
<input type="text" name="text" id="text">
<input type="submit" value="Send" id="submit">
</form>
我需要从文件中下载所有消息并选择最后一个消息(最后一小时)。
在for
循环中,您希望从所有邮件中选择每个"time"
,并使用"now"
计算差异。所以我需要尝试这样做,但我认为for loop
不正确。出了什么问题?