Skip to content

Instantly share code, notes, and snippets.

@jeankassio
Created October 16, 2023 15:36
Show Gist options
  • Save jeankassio/a1a6e58a4301b977d818ccbb79c92689 to your computer and use it in GitHub Desktop.
Save jeankassio/a1a6e58a4301b977d818ccbb79c92689 to your computer and use it in GitHub Desktop.
Send Typing each 10 seconds with pause if no typing after 2 seconds. (keyup keydown event)
function TypingMessage(){
if(typeof(window.Typing) == 'undefined' || window.Typing == null){
console.log("Dispara Evento Escrevendo");
window.Typing = new TypingTimer(function(){
console.log("Dispara Evento Escrevendo");
}, 10000);
}
if(window.Typing.remaining() < 0){
window.Typing.start();
}
}
function NoTypingMessage(){
window.NoTyping = setTimeout(function(){
window.Typing.stop();
window.Typing = null;
console.log("Dispara Evento Pause");
}, 2000);
}
function TypingTimer(callback, delay){
let id, started;
this.start = function(){
started = new Date();
id = setTimeout(callback, delay);
}
this.stop = function(){
clearTimeout(id);
}
this.remaining = function (){
return (delay - (new Date() - started));
}
this.start();
}
$(document).on("keyup keydown paste", "#boxmessage", function(e){
if(typeof(window.NoTyping) != 'undefined'){
clearTimeout(window.NoTyping);
}
NoTypingMessage();
if($(this).val != ""){
TypingMessage();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment