Javascript jQuery Ajax 方法并不总是有效
Javascript jQuery Ajax method not always working
>我正在开发一个聊天应用程序,发现了一个我无法解决的错误,我卡住了:/
基本上:我有两个不同的 LocalStorage 变量(kontakte
,当您向以前从未写过信的人写消息时,将添加一个新联系人,"chat_" + email
,有您和您的当前联系人写的所有消息)。我的方法将每 4.5 秒运行一次,它会测试是否有新消息 ( data != null
),当这是真的时,该方法将查看您当前是否在聊天页面上收到新消息 ( data[neuenabf - 4] == seite5_empfemail && seiten_aktpossonder == 5
)。
如果这也是真的,它将新消息添加到聊天中(seite_5main.innerHTML = seite_5main.innerHTML
...),当消息开始等于"!§?%&&45es","!§?%&&45ds","!§?%&&45as"时,会显示一个额外的框(弹出窗口)
这就是问题所在,大约每 4 - 5 次聊天中就会显示一条新消息。此外,当您查看 ajax 请求时,您无法在 Firefox F12 控制台中看到此新消息(每 4 - 5 条消息您看不到第 4/5 条消息)。那么更疯狂的是,新消息肯定会添加到消息列表中(本地存储"chat_"+电子邮件)!?
方法:
var seite_5nnnspeicher1 = [];
var seite5_spielzeitb;
function neuenachrichtabf() {
$.ajax({
dataType: "JSON",
type: 'POST',
url: 'http://...de/ajax/chat.php',
data: {sender: LOCemail, methode: "s2"},
success: function (data) {
if (data != null) {
for (neuenabf = data.length-1; neuenabf > 0; neuenabf = neuenabf - 5) {
if ((data[neuenabf-4] == seite5_empfemail) && seiten_aktpossonder == 5) {//Wenn eine neue Nachricht kommt und man aktuell bei dem Kontakt ist wo eine neue Nachricht kommt
if (data[neuenabf-1].startsWith("!§?%&&45es")) {
seite5_spielzeitb = 'Um '+data[neuenabf-1].slice(16, 19)+data[neuenabf-1].slice(19, 21)+' Uhr, den '+data[neuenabf-1].slice(10, 12)+'-'+data[neuenabf-1].slice(13, 15);
seite_5main.innerHTML = seite_5main.innerHTML + '<div id="spielanfrage_s5bekommen" style="background: white; font-size: 15px; position: relative; margin-top: 8px; margin-left: 12px; border-radius: 2px; padding: 8px; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2); color: black; display: table-cell; max-width: 80%; white-space: normal; float: left; clear: both;"><font style="color: #4285f4;"><b>Spielanfrage:</b></font></br><font style="color: #656565;">'+seite5_spielzeitb+'</font></br><div style="width: 50%; height: 40px; color: #656565; float: left; line-height: 40px;" onclick="s5_spabl('''+seite5_spielzeitb+''')">       <img src="img/delete.png" style="position: absolute; height: 15px; margin-top: 12px;"></div><div style="width: 50%; height: 40px; float: left; color: #656565; line-height: 40px; position: relative;" onclick="s5_spakt('''+seite5_spielzeitb+''')">       <img src="img/done.png" style="position: absolute; height: 24px; top: 8px;"></div><font style="color: #ccc; font-size: 11px; float: right;">'+data[neuenabf]+'</font><div class="arrow-left" style="position: absolute; top: 0px; left: -9px;"></div></div>';
}
else if(data[neuenabf-1].startsWith("!§?%&&45ds"))
{
seite5_spielzeitb = 'Um '+data[neuenabf-1].slice(16, 19)+data[neuenabf-1].slice(19, 21)+' Uhr, den '+data[neuenabf-1].slice(10, 12)+'-'+data[neuenabf-1].slice(13, 15);
document.getElementById("spielanfrage_s5geschickt").innerHTML = '<font style="color: #4285f4;"><b>Spielanfrage:</b></br></font><font style="color: #656565;">'+seite5_spielzeitb+'</font></br></br><font style="color: #f44336; text-align: center;"><italic>Abgelehnt</italic></font><div class="arrow-right" style="position: absolute; bottom: 0px; right: -9px;"></div>';
document.getElementById("spielanfrage_s5geschickt").id = '';
s5_spabls = JSON.parse(localStorage.getItem("chat_"+seite5_empfemail));
index_ersetzen(s5_spabls, "ds");
}
else if(data[neuenabf-1].startsWith("!§?%&&45as"))
{
seite5_spielzeitb = 'Um '+data[neuenabf-1].slice(16, 19)+data[neuenabf-1].slice(19, 21)+' Uhr, den '+data[neuenabf-1].slice(10, 12)+'-'+data[neuenabf-1].slice(13, 15);
document.getElementById("spielanfrage_s5geschickt").innerHTML = '<font style="color: #4285f4;"><b>Spielanfrage:</b></br></font><font style="color: #656565;">'+seite5_spielzeitb+'</font></br></br><font style="color: #009688;">Angenommen</font><div class="arrow-right" style="position: absolute; bottom: 0px; right: -9px;"></div>';
document.getElementById("spielanfrage_s5geschickt").id = '';
s5_spabls = JSON.parse(localStorage.getItem("chat_"+seite5_empfemail));
index_ersetzen(s5_spabls, "as");
}
else
{
seite_5main.innerHTML = seite_5main.innerHTML + '<div style="background: white; font-size: 15px; position: relative; margin-top: 8px; margin-left: 12px; border-radius: 2px; padding: 8px; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2); color: black; display: table-cell; max-width: 80%; white-space: normal; float: left; clear: both;">'+data[neuenabf-1]+'</br><font style="color: #ccc; font-size: 11px; float: left;">'+data[neuenabf]+'</font><div class="arrow-left" style="position: absolute; top: 0px; left: -9px;"></div></div>';
}
//ZUR LOKALEN STORAGE HINZUFÜGEN
if (localStorage.getItem("chat_"+seite5_empfemail))
{
seite_5nnnspeicher1 = JSON.parse(localStorage.getItem("chat_"+seite5_empfemail));
seite_5nnnspeicher1.push(seite5_empfemail, data[neuenabf], data[neuenabf-1]);
localStorage.setItem("chat_"+seite5_empfemail, JSON.stringify(seite_5nnnspeicher1));
}
else
{
localStorage.setItem("chat_"+seite5_empfemail, JSON.stringify([seite5_empfemail, data[neuenabf], data[neuenabf-1]]));
}
if (localStorage.getItem("kontakte"))
{
seite_5nnnspeicher = JSON.parse(localStorage.getItem("kontakte"));
if(index_finden(seite_5nnnspeicher, seite5_empfemail) === 1) //Wenn der aktuelle Kontakt schon in den Kontakten ist -> Nichts passiert
{
}
else //Wenn der aktuelle noch nicht in den Kontakten ist -> Kontakt hinzufügen
{
seite_5nnnspeicher.push(seite5_empfemail, seite_5name, seite_5lk, 'a');
localStorage.setItem("kontakte", JSON.stringify(seite_5nnnspeicher));
}
}
else //Wenn noch keine Kontakte da sind -> Kontakt hinzufügen
{
localStorage.setItem("kontakte", JSON.stringify([seite5_empfemail, seite_5name, seite_5lk, 'a']));
}
}
//---------//
else
{
if (localStorage.getItem("chat_"+data[neuenabf-4]))
{
seite_5nnnspeicher1 = JSON.parse(localStorage.getItem("chat_"+data[neuenabf-4]));
seite_5nnnspeicher1.push(data[neuenabf-4], data[neuenabf], data[neuenabf-1]);
localStorage.setItem("chat_"+data[neuenabf-4], JSON.stringify(seite_5nnnspeicher1));
}
else
{
localStorage.setItem("chat_"+data[neuenabf-4], JSON.stringify([data[neuenabf-4], data[neuenabf], data[neuenabf-1]]));
}
if (localStorage.getItem("kontakte"))
{
seite_5nnnspeicher = JSON.parse(localStorage.getItem("kontakte"));
if(index_finden(seite_5nnnspeicher, data[neuenabf-4]) === 1) //Wenn der aktuelle Kontakt schon in den Kontakten ist -> Nichts passiert
{
}
else //Wenn der aktuelle noch nicht in den Kontakten ist -> Kontakt hinzufügen
{
seite_5nnnspeicher.push(data[neuenabf-4], data[neuenabf-3], data[neuenabf-2], 'a');
localStorage.setItem("kontakte", JSON.stringify(seite_5nnnspeicher));
}
}
else //Wenn noch keine Kontakte da sind -> Kontakt hinzufügen
{
localStorage.setItem("kontakte", JSON.stringify([data[neuenabf-4], data[neuenabf-3], data[neuenabf-2], 'a']));
}
//---//
if (localStorage.getItem("LOCneuenawo_"+data[neuenabf-4]))
{
localStorage.setItem("LOCneuenawo_"+data[neuenabf-4], parseInt(localStorage.getItem("LOCneuenawo_"+data[neuenabf-4]))+1);
}
else
{
localStorage.setItem("LOCneuenawo_"+data[neuenabf-4], "1");
}
document.getElementById("main_menu_nkreis").style.visibility = "visible";
}
}
seite4_kontakteanz();
$('#seite_5main').scrollTop(1000000);
}
},
error: function()
{
}
});
}
function index_finden(dasarray,wert)
{
for (var i = 0; i < dasarray.length; i++)
{
if (dasarray[i] === wert)
{
return 1;
}
}
}
怎么可能,不是不可能吗?
Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
引用 http://api.jquery.com/jquery.ajax/
所以更好地使用模式:
$.ajax({
dataType: "JSON",
type: 'POST',
url: 'http://...de/ajax/chat.php',
data: {
sender: LOCemail,
methode: "s2"
},
})
.error(function( err ) {
alert( "error: " + err );
});
.done(function( data ) {
// Your function as above
});
正如您在上面评论的那样,没有 ajax 错误,所以我想您的脚本中还有其他问题,应该在其他问题中讨论。
如果你喜欢给你的html和css提供一个摆弄。我会看看那个
- jquery代码在Mozilla中有效,但在其他浏览器上无效
- 使用条件语句,使 jquery fadeIn/fadeOut 仅在视口 >= 480px 时才有效
- 在jQuery中创建向下滑动子菜单的最有效方法
- jquery.html有效,但是.add不行;t
- jquery ajax”;SyntaxError:意外的输入结束“;基于有效的JSON
- 如何在jQuery中以有效的方式创建链接
- jQuery"焦点”;在一个实例中有效,但在其他实例中无效
- javascript/jQuery:在title中添加换行符——在IE9中有效,但在IE8中无效
- slack files.upload-如何从javascript/jQuery将文件作为有效负载发送
- jQuery变形按钮概念-fadeIn()并不总是有效
- 最有效的jquery显示/隐藏切换
- 为什么form.submit()在使用jQuery时有效,而在使用普通JS时无效
- 只有第一个按钮在带有 Jquery 的 forEach 标签中有效
- 使用 jQuery 调用 PHP 端点的最有效方法
- 在.load和.getScript之后的jQuery绑定仅在警报之后有效
- 在Knockout视图模型中调用jQuery插件是一种有效的模式
- JQuery-检查IMG src是否有效
- 为什么不;t Jquery有效,而js有效
- 检查是否有效Jquery日期
- 如何使这些重复的代码更有效:jquery