Debounce input key press handling (#5309)

- input key handler causes 2 layout cycles on each keypress which can
clog up browser rendering when typing quickly
- this change adds a debounce to the key press handler of 500ms

Fixes #5308
Signed-off-by: David Kaltschmidt <david.kaltschmidt@gmail.com>
This commit is contained in:
David 2019-03-06 16:16:55 +01:00 committed by Julius Volz
parent 225bc77448
commit 38fea83c1f
2 changed files with 20 additions and 3 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,7 @@
var Prometheus = Prometheus || {}; var Prometheus = Prometheus || {};
var graphTemplate; var graphTemplate;
var INPUT_DEBOUNCE_WAIT = 500; // ms
var SECOND = 1000; var SECOND = 1000;
/** /**
@ -75,7 +76,9 @@ Prometheus.Graph.prototype.initialize = function() {
self.queryForm.submit(); self.queryForm.submit();
e.preventDefault(); e.preventDefault();
} }
$(this).on('keyup input', function() { resizeTextarea(this); }); $(this).on('keyup input', debounce(function() {
resizeTextarea(this);
}, INPUT_DEBOUNCE_WAIT));
}); });
self.expr.click(function(e) { self.expr.click(function(e) {
@ -1159,6 +1162,20 @@ const queryHistory = {
} }
}; };
// Defers invocation of fn for waitPeriod if returned function is called repeatedly
function debounce(fn, waitPeriod) {
var timeout;
return function() {
clearTimeout(timeout);
var args = arguments;
var scope = this;
function invokeFn() {
return fn.apply(scope, args);
}
timeout = setTimeout(invokeFn, waitPeriod);
};
}
function escapeHTML(string) { function escapeHTML(string) {
var entityMap = { var entityMap = {
"&": "&amp;", "&": "&amp;",