add extension for typing delay

This commit is contained in:
Ryan Peters 2021-07-21 11:00:05 -04:00
parent a1e97d6063
commit 92559fd7df
4 changed files with 44 additions and 3 deletions

View File

@ -16,8 +16,8 @@
<nav class="navbar navbar-expand navbar-light justify-content-between"> <nav class="navbar navbar-expand navbar-light justify-content-between">
<a class="navbar-brand" href="#">HTML 101</a> <a class="navbar-brand" href="#">HTML 101</a>
<form class="form-inline" action=""> <form class="form-inline" action="">
<input type="text" class="form-control mr-sm-2" id="your-name-text" placeholder="Enter your name" /> <input type="text" class="form-control mr-sm-2" id="your-name-text" placeholder="Enter your name!" />
<input type="text" class="form-control" maxlength="6" id="your-color-text" placeholder="Color" /> <input type="text" class="form-control" id="your-color-text" placeholder="Color?" />
</form> </form>
<div class="collapse navbar-collapse"> <div class="collapse navbar-collapse">
<ul class="navbar-nav ml-auto"> <ul class="navbar-nav ml-auto">
@ -42,6 +42,7 @@
</div> </div>
<script src="~/lib/jquery/dist/jquery.min.js"></script> <script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/js/extensions.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script> <script src="~/js/site.js" asp-append-version="true"></script>

View File

@ -22,6 +22,10 @@ textarea, .navbar-brand {
line-height: 18px; line-height: 18px;
} }
#your-name-text, #your-color-text {
font-weight: bold;
}
#your-color-text { #your-color-text {
width: 90px; width: 90px;
text-align: center; text-align: center;

View File

@ -0,0 +1,36 @@
; (function ($) {
$.fn.extend({
doneTyping: function (callback, timeout) {
timeout = timeout || 500; // 500 ms default timeout
var timeoutReference,
doneTyping = function (el) {
if (!timeoutReference) return;
timeoutReference = null;
callback.call(el);
};
return this.each(function (i, el) {
var $el = $(el);
// Chrome Fix (Use keyup over keypress to detect backspace)
// thank you @palerdot
$el.is(':input') && $el.on('keyup keypress paste', function (e) {
// This catches the backspace button in chrome, but also prevents
// the event from triggering too preemptively. Without this line,
// using tab/shift+tab will make the focused element fire the callback.
if (e.type == 'keyup' && e.keyCode != 8) return;
// Check if timeout has been set. If it has, "reset" the clock and
// start over again.
if (timeoutReference) clearTimeout(timeoutReference);
timeoutReference = setTimeout(function () {
// if we made it here, our timeout has elapsed. Fire the
// callback
doneTyping(el);
}, timeout);
}).on('blur', function () {
// If we can, fire the event since we're leaving the field
doneTyping(el);
});
});
}
});
})(jQuery);

View File

@ -34,7 +34,7 @@
// events // events
$(window).on('hashchange', isLiveMode); $(window).on('hashchange', isLiveMode);
$('#your-name-text, #your-color-text').on('blur', sendName); $('#your-name-text, #your-color-text').doneTyping(sendName);
let isTeacher = window.location.hash === '#4ELQcUq7UnFGghAZCVr4Chr9JmtncigfkGu5WSf9'; let isTeacher = window.location.hash === '#4ELQcUq7UnFGghAZCVr4Chr9JmtncigfkGu5WSf9';