add timer to search to reduce cpu usage
This commit is contained in:
parent
79f5acd020
commit
48268770b7
14
search.js
14
search.js
@ -3,6 +3,9 @@ const PATH = "path"
|
|||||||
const TAGS = "tags"
|
const TAGS = "tags"
|
||||||
const TITLE = "title"
|
const TITLE = "title"
|
||||||
|
|
||||||
|
const SEARCH_TIMEOUT_MS = 100
|
||||||
|
var SEARCH_TIMEOUT_ID = -1
|
||||||
|
|
||||||
const fuse = new Fuse(data, {
|
const fuse = new Fuse(data, {
|
||||||
keys: [
|
keys: [
|
||||||
{
|
{
|
||||||
@ -35,7 +38,14 @@ const resultsDiv = document.getElementById('results')
|
|||||||
|
|
||||||
var results = []
|
var results = []
|
||||||
|
|
||||||
|
function updateResultsWithTimeout() {
|
||||||
|
console.log("clearing timeout")
|
||||||
|
if (SEARCH_TIMEOUT_ID) SEARCH_TIMEOUT_ID = clearTimeout(SEARCH_TIMEOUT_ID)
|
||||||
|
SEARCH_TIMEOUT_ID = setTimeout(updateResults, SEARCH_TIMEOUT_MS)
|
||||||
|
}
|
||||||
|
|
||||||
function updateResults() {
|
function updateResults() {
|
||||||
|
console.log("updating results")
|
||||||
resultsDiv.innerHTML = ''
|
resultsDiv.innerHTML = ''
|
||||||
results = fuse.search(searchBar.value, { limit: RESULTS_MAX })
|
results = fuse.search(searchBar.value, { limit: RESULTS_MAX })
|
||||||
results.forEach(r => {
|
results.forEach(r => {
|
||||||
@ -99,10 +109,10 @@ searchBar.addEventListener('keyup', e => {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
updateResults()
|
updateResultsWithTimeout()
|
||||||
})
|
})
|
||||||
|
|
||||||
searchBar.addEventListener('change', updateResults)
|
searchBar.addEventListener('change', updateResultsWithTimeout)
|
||||||
|
|
||||||
const searchParams = new URL(window.location.href).searchParams;
|
const searchParams = new URL(window.location.href).searchParams;
|
||||||
searchBar.value = searchParams.get('q');
|
searchBar.value = searchParams.get('q');
|
||||||
|
Reference in New Issue
Block a user