Put input timeout clearance inside a function

This commit is contained in:
Guillaume Gomez 2020-05-31 14:27:33 +02:00
parent bcf57d8f20
commit 7a2efa3a10

View File

@ -92,6 +92,13 @@ function getSearchElement() {
var titleBeforeSearch = document.title; var titleBeforeSearch = document.title;
function clearInputTimeout() {
if (searchTimeout !== null) {
clearTimeout(searchTimeout);
searchTimeout = null;
}
}
function getPageId() { function getPageId() {
var id = document.location.href.split("#")[1]; var id = document.location.href.split("#")[1];
if (id) { if (id) {
@ -345,10 +352,7 @@ function getSearchElement() {
if (hasClass(help, "hidden") === false) { if (hasClass(help, "hidden") === false) {
displayHelp(false, ev, help); displayHelp(false, ev, help);
} else if (hasClass(search, "hidden") === false) { } else if (hasClass(search, "hidden") === false) {
if (searchTimeout !== null) { clearInputTimeout();
clearTimeout(searchTimeout);
searchTimeout = null;
}
ev.preventDefault(); ev.preventDefault();
hideSearchResults(search); hideSearchResults(search);
document.title = titleBeforeSearch; document.title = titleBeforeSearch;
@ -1805,7 +1809,7 @@ function getSearchElement() {
function startSearch() { function startSearch() {
var callback = function() { var callback = function() {
clearTimeout(searchTimeout); clearInputTimeout();
if (search_input.value.length === 0) { if (search_input.value.length === 0) {
if (browserSupportsHistoryApi()) { if (browserSupportsHistoryApi()) {
history.replaceState("", window.currentCrate + " - Rust", "?search="); history.replaceState("", window.currentCrate + " - Rust", "?search=");
@ -1819,10 +1823,7 @@ function getSearchElement() {
search_input.oninput = callback; search_input.oninput = callback;
document.getElementsByClassName("search-form")[0].onsubmit = function(e) { document.getElementsByClassName("search-form")[0].onsubmit = function(e) {
e.preventDefault(); e.preventDefault();
if (searchTimeout !== null) { clearInputTimeout();
clearTimeout(searchTimeout);
searchTimeout = null;
}
search(); search();
}; };
search_input.onchange = function(e) { search_input.onchange = function(e) {
@ -1831,10 +1832,7 @@ function getSearchElement() {
return; return;
} }
// Do NOT e.preventDefault() here. It will prevent pasting. // Do NOT e.preventDefault() here. It will prevent pasting.
if (searchTimeout !== null) { clearInputTimeout();
clearTimeout(searchTimeout);
searchTimeout = null;
}
// zero-timeout necessary here because at the time of event handler execution the // zero-timeout necessary here because at the time of event handler execution the
// pasted content is not in the input field yet. Shouldnt make any difference for // pasted content is not in the input field yet. Shouldnt make any difference for
// change, though. // change, though.