我正在创建聊天,但每次我发送一条消息时,它都不会让我发送另一条消息,比如表格不起作用?
I am creating a chat, but everytime I send a message, it will not let me send another message, like the form won't work?
当我 post 一条消息进入我的聊天时,它不会让我再次 post。我相信这是与发送相关的代码:
window.typing = false;
var posted = 0;
var canPost = 1;
var donotrefresh = 0;
document.forms['send'].addEventListener('submit', function (event) {
event.preventDefault();
if (!this.message.value || !canPost) {
return 0;
}
var form = this;
var message = this.message.value;
this.message.value = "";
this.message.focus();
var ajax = new XMLHttpRequest();
posted = 1;
canPost = 0;
chat.refresh();
chat.messages.scrollTop = 0;
ajax.onreadystatechange = function () {
if (ajax.readyState >= 4 && ajax.status == 200) {
if (!ajax.responseText) {
canPost = 1;
posted = 0;
}
}
}
ajax.open("GET", "/chat/ajax.php?req=post&chatmsg=" + encodeURIComponent(message), true);
ajax.send(null);
}, false);
这是我的提交代码:
<form name="send" style="height:100px;width:100%">
<input type="text" name="message" style="width:60%;height:2em;resize:vertical;opacity:0.8;" maxlength="220" placeholder="Your message to send..." required />
<input type="submit" value="Send" style="vertical-align:top;opacity:0.8;" />
</form>
尝试移除 if (!ajax.responseText)
ajax.onreadystatechange = function () {
if (ajax.readyState >= 4 && ajax.status == 200) {
canPost = 1;
posted = 0;
}
}
您需要 "posted" 变量吗?看起来代码不再使用它了。 canPost 的目的在我看来是为了避免同时请求。在这里真的要避免这种情况吗?
当我 post 一条消息进入我的聊天时,它不会让我再次 post。我相信这是与发送相关的代码:
window.typing = false;
var posted = 0;
var canPost = 1;
var donotrefresh = 0;
document.forms['send'].addEventListener('submit', function (event) {
event.preventDefault();
if (!this.message.value || !canPost) {
return 0;
}
var form = this;
var message = this.message.value;
this.message.value = "";
this.message.focus();
var ajax = new XMLHttpRequest();
posted = 1;
canPost = 0;
chat.refresh();
chat.messages.scrollTop = 0;
ajax.onreadystatechange = function () {
if (ajax.readyState >= 4 && ajax.status == 200) {
if (!ajax.responseText) {
canPost = 1;
posted = 0;
}
}
}
ajax.open("GET", "/chat/ajax.php?req=post&chatmsg=" + encodeURIComponent(message), true);
ajax.send(null);
}, false);
这是我的提交代码:
<form name="send" style="height:100px;width:100%">
<input type="text" name="message" style="width:60%;height:2em;resize:vertical;opacity:0.8;" maxlength="220" placeholder="Your message to send..." required />
<input type="submit" value="Send" style="vertical-align:top;opacity:0.8;" />
</form>
尝试移除 if (!ajax.responseText)
ajax.onreadystatechange = function () {
if (ajax.readyState >= 4 && ajax.status == 200) {
canPost = 1;
posted = 0;
}
}
您需要 "posted" 变量吗?看起来代码不再使用它了。 canPost 的目的在我看来是为了避免同时请求。在这里真的要避免这种情况吗?