Skip to content

Commit

Permalink
Add debounce to search
Browse files Browse the repository at this point in the history
Should reduce number of search calls
  • Loading branch information
pehala committed Jan 28, 2024
1 parent 382993d commit 22f85a3
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions backend/templates/songs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ <h6 class="d-inline" ><%:author%></h6>
import {Options, BooleanOption} from "{% static 'js/Options.js' %}"
const canWakeLock = () => 'wakeLock' in navigator;

function debounce(callback, delay = 1000) {
var time;

return (...args) => {
clearTimeout(time);
time = setTimeout(() => {
callback(...args);
}, delay);
};
}

let table = null
function createDatatable(paging) {
return $('#datatable').DataTable({
Expand Down Expand Up @@ -226,6 +237,14 @@ <h6 class="d-inline" ><%:author%></h6>
$(".chord").show()
}
}
const search = debounce(
function ( val ) {
table.search(val);
table.draw(false);
console.log("searched")
}, 200
);

const config = new Map([
["hide_chords", new BooleanOption(document.getElementById("hideChords"), hideChords, false)],
["prevent_sleep", new BooleanOption(document.getElementById("preventSleep"), function(value) {
Expand Down Expand Up @@ -280,9 +299,8 @@ <h6 class="d-inline" ><%:author%></h6>

});

document.getElementById("searchInput").addEventListener("input", function (event) {
table.search(event.target.value);
table.draw();
document.getElementById("searchInput").addEventListener("input", function(event) {
search(event.target.value)
})
</script>
{% endblock %}

0 comments on commit 22f85a3

Please sign in to comment.