Created
October 16, 2023 15:36
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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