Я делаю чат и у меня появилась проблема он очень тормозной.
Js(самый важный код):
var tim = 1000;
var messages = document.getElementById("messages");
var title = 0;
var quest = document.getElementById("quest");//вибір: вхід, реєстрація
if(auth == true){
messages.style.opacity = "1";
var title_timer = setInterval(titleReload,tim);
setInterval(ban_check,tim);
}
else{
quest.style.display = "block";
black.style.display = "block";
messages.style.opacity = "0";
}
//- перевірка звання --
function titleReload(){
$.ajax({
url:"title_reload.php",
type:"POST",
data: ({username:name}),
dataType:"html",
success:titleUpdate
});
}
function titleUpdate(data){
var title = data;
if(title == 1 || title == 2){
if($("#panel").html() == '<button onclick="pause(\'clear\')" id="pause">Пауза</button><button onclick="pause(1000)" id="continue">Продолжить</button><button onclick="exit_u(\'$username\',\'.$title.\')" id="exit">Выход</button>'){
}
else{
$("#panel").html('<button onclick="pause(\'clear\')" id="pause">Пауза</button><button onclick="pause(1000)" id="continue">Продолжить</button><button onclick="exit_u(\'$username\',\'.$title.\')" id="exit">Выход</button>');
}
}
else{
if($("#panel").html() == '<button onclick="exit_u(\'$username\',\'.$title.\')" id="exit">Выход</button>'){
}
else{
$("#panel").html('<button onclick="exit_u(\'$username\',\'.$title.\')" id="exit">Выход</button>');
}
}
chatReload(title);
return title;
}
function al(){
alert(title);
}
//- відправка повідомлення --
function chatReload(title){
var id = $(".text_message:first").attr("id");
$.ajax({
url:"load.php",
type:"POST",
data: ({title:title,id:id}),
dataType:"html",
success:chatUpdate
});
}
function chatUpdate(data){
if($("#messages").html() == data){
// alert('f')
}
else{
$("#messages").html(data);
}
// alert(id);
}
var submit = document.querySelector("#submit");
function message_clear(){
document.querySelector('input#text').value = "";
}
//- перевірка бану --
function ban_check(){
$.ajax({
url:"ban_check.php",
type:"POST",
data: ({username:name}),
dataType:"html",
success:ban_check_end
});
}
function ban_check_end(data){
data = JSON.parse(data);
if(data['ban'] == 1){
black.style.display = "block";
delay.style.display = "block";
delay.style.textAlign = "center";
delay.innerHTML = "На вас наложен бан на "+ data['banName'] + ". <br><br> Бан закончится "+ data['banEnd'] + ".";
}
else{
if(auth == true){
if(black.style.display == "none"){
}
else{
black.style.display = "none";
}
if(delay.style.display == "none"){
}
else{
delay.style.display = "none";
}
}
else{
quest.style.display="block";
black.style.display = "block";
messages.style.opacity = "0";
}
}
}
//- Звання --
function title_user(user_id,title_num){
let userID = user_id;
let titleNum = title_num;
$.ajax({
url:"title.php",
type:"POST",
data: ({userID: userID,titleNum:titleNum}),
dataType:"html",
success:title_end
});
}
function title_end(data){
alert(data);
}
index.php:
<?php
session_start();
if(isset($_COOKIE["username4"])){
$username = $_COOKIE["username4"];
//$title = $_COOKIE["title4"];
echo "<script>
var auth = true;
var name = '".$username."';
//var title = '".$title."';
</script>";
}
else {
echo"<script>
var auth = false;
</script>";
}
?>
<h1>Чат</h1>
<div id="chat">
<div id="messages">
</div>
<div id="send">
<input type="text" name="text" id="text" placeholder="Написать сообщение" value=""/>
<input type="submit" value="Отправить" id="submit" />
</div>
<div id="panel">
</div>
</div>
<script type="text/javascript" src="res/lib/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="res/scripts/chat2.js"></script>
load.php:
<?php
class Message {
public function mess1($userid,$username,$idmessage, $text_message,$time){
echo '<div class="mes_wrap"><div class="panel"><div class="option">•••</div><div class="move"><div class="mover_wrap"><div class="option_punkt ban"><div class="punkt_text ban_text">Бан</div><div class="punkt_punkt ban_time" onclick="ban_user('.$userid.',3600)">1 час</div><div class="punkt_punkt ban_time" onclick="ban_user('.$userid.',43200)">12 часов</div><div class="punkt_punkt ban_time" onclick="ban_user('.$userid.',86400)">24 часа</div><div class="punkt_punkt ban_time" onclick="ban_user('.$userid.',604800)">1 неделя</div><div class="punkt_punkt ban_time" onclick="ban_user('.$userid.',2592000)">1 месяц</div><div class="punkt_punkt ban_time" onclick="ban_user('.$userid.',31536000)">1 год</div></div><div class="option_punkt title"><div class="punkt_text title_text">Звание</div><div class="punkt_punkt titles" onclick="title_user('.$userid.', 1)">Админ</div><div class="punkt_punkt titles" onclick="title_user('.$userid.', 2)">Модератор</div><div class="punkt_punkt titles" onclick="title_user('.$userid.', 3)">Юзер</div></div></div></div></div><div class="username">'.$username.'</div><p class="text_message" id="'.$idmessage.'">'.$text_message.'</p><div class="time">'.$time.'</div><div class="delete" id="'.$idmessage.'" onclick="delete_message('.$idmessage.')">x</div></div>';
}
public function mess2($userid,$username,$idmessage, $text_message,$time){
echo '<div class="mes_wrap"><div class="panel"><div class="option">•••</div><div class="move"><div class="mover_wrap"><div class="option_punkt ban ban_moderator"><div class="punkt_text ban_text">Бан</div><div class="punkt_punkt ban_time" onclick="ban_user('.$userid.',3600)">1 час</div><div class="punkt_punkt ban_time" onclick="ban_user('.$userid.',43200)">12 часов</div><div class="punkt_punkt ban_time" onclick="ban_user('.$userid.',86400)">24 часа</div></div><div class="option_punkt title title_moderator"><div class="punkt_text title_text">Звание</div><div class="punkt_punkt titles" onclick="title_user('.$userid.', 2)">Модератор</div><div class="punkt_punkt titles" onclick="title_user('.$userid.', 3)">Юзер</div></div></div></div></div><div class="username">'.$username.'</div><p class="text_message" id="'.$idmessage.'">'.$text_message.'</p><div class="time">'.$time.'</div><div class="delete" id="'.$idmessage.'" onclick="delete_message('.$idmessage.')">x</div></div>';
}
public function mess3($username,$idmessage, $text_message,$time){
echo '<div class="mes_wrap"><div class="username">'.$username.'</div><p class="text_message" id="'.$idmessage.'">'.$text_message.'</p><div class="time">'.$time.'</div></div>';
}
}
$message = new Message();
//- подключился к бд ..--
$idmax = $_POST["title"];
$query = "SELECT * FROM `mess` ORDER BY UTime DESC";
$read = mysqli_query($link,$query);
$title = $_POST["title"];
// echo "<script>alert(".$title.");</script>";
while ($row = mysqli_fetch_array($read)){
$query1 = "SELECT * FROM `chat_reg` WHERE UName='".$row['UName']."'";
$read1 = mysqli_query($link,$query1);
$row1 = mysqli_fetch_array($read1);
if($title == 1){
$message->mess1($row1["id"],$row["UName"],$row["id"],$row["UMessage"],$row["UTime"]);
}
if($title == 2){
$message->mess2($row1["id"],$row["UName"],$row["id"],$row["UMessage"],$row["UTime"]);
}
if($title == 3){
$message->mess3($row["UName"],$row["id"],$row["UMessage"],$row["UTime"]);
}
}
?>
ban_check.php:
<?php
//....
mysqli_set_charset($link,"utf8mb4");
if(isset($_POST["username"])){
$username = $_POST["username"];
$query = "SELECT * FROM `chat_reg` WHERE UName='$username'";
$read = mysqli_query($link,$query);
$row = mysqli_fetch_array($read);
$tel = $row['UTelephone'];
if($row["UBan"] == 1){
$query1 = "SELECT * FROM `ban_users` WHERE UTelephone='$tel'";
$read1 = mysqli_query($link,$query1);
$row1 = mysqli_fetch_array($read1);
$query2 = "SELECT TIMESTAMP(NOW())";
$read2 = mysqli_query($link,$query2);
$row_curtime = mysqli_fetch_array($read2);
if($row_curtime["0"] < $row1["UEnd"]){
echo json_encode(array('ban'=>1,'banName' => $row1['UBanName'],'banEnd'=> $row1['UEnd']));
}
else {
$query3 = "UPDATE `chat_reg` SET `UBan` = '0' WHERE UName = '$username'";
$record = mysqli_query($link,$query3);
}
}
else{
echo json_encode(array('ban'=>0));
}
}
?>
title_reload.php:
<?php
//..
mysqli_set_charset($link,"utf8mb4");
if(isset($_POST["username"])){
$nam = $_POST["username"];
$query1 = "SELECT * FROM `chat_reg` WHERE UName='$nam'";
$read = mysqli_query($link,$query1);
$row = mysqli_fetch_array($read);
$title_1 = "SELECT * from chat_reg WHERE UTitle=1 AND UName='$nam'";
$title_2 = "SELECT * from chat_reg WHERE UTitle=2 AND UName='$nam'";
$title_3 = "SELECT * from chat_reg WHERE UTitle=3 AND UName='$nam'";
$check_title_1 = mysqli_query($link,$title_1);
$check_title_2 = mysqli_query($link,$title_2);
$check_title_3 = mysqli_query($link,$title_3);
$row_num_title_1 = mysqli_num_rows($check_title_1);
$row_num_title_2 = mysqli_num_rows($check_title_2);$row_num_title_3 = mysqli_num_rows($check_title_3);
if($row_num_title_1 == 1){
$title = 1;
setcookie('title4', $title,time()+(86400*1),"/");
echo $title;}
if($row_num_title_2 == 1){
$title = 2;
setcookie('title4', $title,time()+(86400*30),"/");
echo $title;}
if($row_num_title_3 == 1){
$title = 3;
setcookie('title4', $title,time()+(86400*30),"/");
echo $title;}}
?>
Как можно оптимизировать его?