js setTimeout function runs every 3s to receive messages , can convert it to Ajax Get ? [closed]

i have a messages chat on my video site .
it is not a comments , it is a chat runs in real time between users .
it is working like this :

  • a setTimeout function runs every 3 seconds all the time .
  • user1 send message .
  • setTimeout function is working so user2 get the message Immediately (3s).
  • setTimeout function working .
  • users send / not send the setTimeout function still working .
  • if stop this setTimeout function user2 will not see the message until refresh the page .

my hosting server company say this cause a heavy load on the cpu .
long polling , websocket , nodejs and other like these methods are not supported on my server .

so the only way i have to do , to convert this kind of code to Ajax .
but i dont have any knowledge of php/js coding .

NOTE: if it is possible to run the setTimeout function only if message sent , this will be great instead of change the code to ajax .

here all my code , php , html and js :
and if you need more codes i will post them

<?php 
if ($first == 'fetch') {
if (empty($_POST['last_id'])) {
$_POST['last_id'] = 0;
}
if (empty($_POST['id'])) {
$_POST['id'] = 0;
}
if (empty($_POST['first_id'])) {
$_POST['first_id'] = 0;
}
$messages_html = PT_GetMessages($_POST['id'], array('last_id' => $_POST['last_id'], 'first_id' => $_POST['first_id'], 'return_method' => 'html'));
if (!empty($messages_html)) {
$html = PT_LoadPage("messages/{$pt->config->server}/messages", array('MESSAGES' => $messages_html));
} else {
$html = PT_LoadPage("messages/ajax/no-messages");
}
$users_html = PT_GetMessagesUserList(array('return_method' => 'html'));
if (!empty($messages_html) || !empty($users_html)) {
$data = array('status' => 200, 'message' => $messages_html, 'users' => $users_html);
}
}
?>

<div class="user-send-message" >                        
<form action="#" method="POST" id="new-message-form" >
<textarea rows="1" name="new-message" id="new-message" ></textarea>
<button type="submit" id="send-button">send</button>
<input type="hidden" id="user-id" name="id" value="<?php echo $pt->chat_id; ?>">
<input type="hidden" id="user-avatar" value="<?php echo ($pt->chat_user->avatar) ? $pt->chat_user->avatar : "";?>">
</form>                 
</div>

<script>
var messagesInterval = <?php echo (!empty($pt->extra_config->ajax_message_update_interval)) ? $pt->extra_config->ajax_message_update_interval : 3000 ?>;
$(function() {
window.UpdateChatU = setTimeout(function () {
fetchMessages();
}, messagesInterval);
});

function fetchMessages() {
clearTimeout(window.UpdateChatU);
$.post('{{LINK aj/messages/fetch}}', {id: $('#user-id').val(), last_id: $('.message:last').attr('data-id')}, function(data, textStatus, xhr) {
if (data.status == 200) {
if (data.message.length > 0) {
$('.messages').append(data.message);
$('.user-messages').scrollTop($('.user-messages')[0].scrollHeight);
}
if ($('#search-list').val() == 0) {
$('.messages-sidebar .list-group').html(data.users);
}
}
window.UpdateChatU = setTimeout(function () {
fetchMessages();
}, messagesInterval);
});
}
$(function() {
form.ajaxForm({
url: '{{LINK aj/messages/new}}?hash=' + $('.main_session').val(),
data: {message_id: $('#message_id').val()},
beforeSubmit: function(formData, jqForm, options) {
if ($('.messages').length == 0) {
$('.user-messages').html('<div class="messages"></div>');
}
if ($('#new-message').val().length >= 1) {
$id = makeid();
formData.push({ name: 'message_id', value: $id });
$('.messages').append('<div class="data_message" data-id="' + $id + '"><div class="message to-user pull-right" data-id=""><div class="user-message">' + nl2br(escapeHTML($('#new-message').val())) + '</div><div class="clear"></div></div><div class="clear"></div></div>');
$('#new-message').val('');
$('#new-message').height(40); 
$('.user-messages').scrollTop($('.user-messages')[0].scrollHeight);
} else {
$('#new-message').val('');
} 
},
success: function(data,rew,rr) {
if (data.status == 400) {
$('.data_message[data-id="' + $id + '"]').remove();
swal({
title: '{{LANG error}}',
text: data.message,
type: 'error',
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'OK',
buttonsStyling: true,
confirmButtonClass: 'btn btn-success',
});
}
else{
$('.data_message[data-id="' + data.message_id + '"]').html(data.message);
}
}
});
});

</script>