I have this code that determines if the user is typing based on this answer . I would like to send a response to the server via ajax, putting my ajax code somewhere in the if / else statement, but I'm not sure how to do this.
var textarea = $('#textarea');
var typingStatus = $('#typing_on');
var lastTypedTime = new Date(0);
var typingDelayMillis = 5000;
function refreshTypingStatus() {
if (!textarea.is(':focus') ||
textarea.val() == '' ||
new Date().getTime() - lastTypedTime.getTime() > typingDelayMillis) {
typingStatus.html('No one is typing -blank space.');
} else {
typingStatus.html('User is typing...');
}
}
function updateLastTypedTime() {
lastTypedTime = new Date();
}
setInterval(refreshTypingStatus, 100);
textarea.keypress(updateLastTypedTime);
textarea.blur(refreshTypingStatus);
What I have tried so far:
var textarea = $('#chatbox');
var typingStatus = $('#typing_on');
var lastTypedTime = new Date(0);
var typingDelayMillis = 5000;
function refreshTypingStatus() {
if (!textarea.is(':focus') ||
textarea.val() == '' ||
new Date().getTime() - lastTypedTime.getTime() > typingDelayMillis) {
typingStatus.html('No one is typing -blank space.');
} else {
typingStatus.html('User is typing...');
$.ajax({
type: "POST",
url: "usertypes.php?v=<?php echo $usarr; ?>",
});
}
}
function updateLastTypedTime() {
lastTypedTime = new Date();
}
setInterval(refreshTypingStatus, 100);
textarea.keypress(updateLastTypedTime);
textarea.blur(refreshTypingStatus);
However mine does not seem to send any response. So I'm not sure if the ajax code is placed correctly. Can someone explain where I am going wrong?
source
share