gpt4 book ai didi

javascript - 为什么我的代码在 Safari 或 Opera 中不起作用?

转载 作者:可可西里 更新时间:2023-11-01 01:50:27 26 4
gpt4 key购买 nike

js中有一个函数它向表显示消息(消息存储在 json 中)。在谷歌浏览器中,它可以工作,但 Safari、Opera 或 Microsoft Edge - 不行!代码中存在与调用 setTimeout (callback, 5000) 相关的错误(没有任何内容发送到回调)。因此,For (var i = 0; i <respond.length; i ++)respond === undefined 起将不起作用.

但为什么会这样呢?

callback(
[{
"time": "1500303264",
"user": "qwe",
"message": "we",
"id": 1
},
{
"time": "1500303987",
"user": "Max",
"message": "q",
"id": 2
}
]);

function smile(mess) {
var smile = ":)";
var graficSmile = "<img src = './image/Smile.png' alt='Smile' align='middle'>";
var string_with_replaced_smile = mess.replace(smile, graficSmile);

var sad = ":("
var graficSad = "<img src = './image/Sad.png' alt='Smile' align='middle'>";
var string_with_replaced_smile_and_sad = string_with_replaced_smile.replace(sad, graficSad);

return string_with_replaced_smile_and_sad;
}

$.getJSON('data/messages.json', callback);
var exists = [];

function callback(respond) {
var timeNow = Date.now();

for (var i = 0; i < respond.length; i++) {
var data = respond[i];

if (exists.indexOf(data.id) != -1) continue;

var timeInMessage = data.time * 1000;
var diff_time = (timeNow - timeInMessage);

if (diff_time <= 3600000) {
var rowClone = $('.mess_hide').clone().removeClass('mess_hide');

var newDate = new Date(timeInMessage);
var dateArray = [newDate.getHours(), newDate.getMinutes(), newDate.getSeconds()]
var res = dateArray.map(function(x) {
return x < 10 ? "0" + x : x;
}).join(":");

$('#messages').append(rowClone);
$('.time', rowClone).html(res);
$('.name', rowClone).html(data.user);
$('.message', rowClone).html(smile(data.message));
$('.scroller').scrollTop($('#messages').height());

exists.push(data.id);
}
}
setTimeout(function(){callback(respond)}, 5000);
}
.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;
}

input#submit {
padding: 0;
margin-left: 21px;
margin-top: 21px;
height: 30px;
width: 95px;
background: #635960;
border: none;
color: white;
font-family: 'Montserrat';
font-size: 16px;
}
<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>
</div>

Chrome Chrome

歌剧 Opera

最佳答案

  1. 因为假定 var 存在 - 数组,但数组 ([]) 的值稍后才分配给它,在调用 $.getJSON(...)。因此,当 callback 第一次被调用时,值 [] 没有设置为 exists。我们只需要移动 var exists callback.
  2. 的第一次调用之上
  3. 当计时器调用callback 时,不会向它传递任何内容。但是计时器需要重新读取文件中的消息并将它们显示在屏幕上。因此,我们需要 setTimeout(function(){callback(respond)}, 5000); 我们需要 setTimeout(function (){$.getJSON('data/messages.json', callback);}, 5000);.

var exists = [];
$.getJSON('data/messages.json', callback);

function callback(respond) {
var timeNow = Date.now();

for (var i = 0; i < respond.length; i++) {
var data = respond[i];

if (exists.indexOf(data.id) != -1) continue;

var timeInMessage = data.time * 1000;
var diff_time = (timeNow - timeInMessage);

if (diff_time <= 3600000) {
var rowClone = $('.mess_hide').clone().removeClass('mess_hide');

var newDate = new Date(timeInMessage);
var dateArray = [newDate.getHours(), newDate.getMinutes(), newDate.getSeconds()]
var res = dateArray.map(function(x) {
return x < 10 ? "0" + x : x;
}).join(":");

$('#messages').append(rowClone);
$('.time', rowClone).html(res);
$('.name', rowClone).html(data.user);
$('.message', rowClone).html(smile(data.message));
$('.scroller').scrollTop($('#messages').height());

exists.push(data.id);
}
}
setTimeout(function() {
$.getJSON('data/messages.json', callback);
}, 5000);
}

关于javascript - 为什么我的代码在 Safari 或 Opera 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45176053/

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