Skip to content

Commit

Permalink
Merge pull request #129 from WildMeOrg/improve_performance_by_adding_…
Browse files Browse the repository at this point in the history
…debounce

add debounce to reduce db requests
  • Loading branch information
TanyaStere42 authored Dec 6, 2024
2 parents b8248c8 + 1cc50c3 commit 1ae5b2f
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,30 @@ $('button#clearSearchInput').on('click',function(e){

$('#tasksPage .datepicker').datepicker();

function debounce(fn, delay, immediate = false) {
let timer;

// Handle image selection modal
$("form#imageSelectionForm input").on("input", async (e) => {
return function (...args) {
const callNow = immediate && !timer;

clearTimeout(timer);

timer = setTimeout(() => {
timer = null;
if (!immediate) fn.apply(this, args);
}, delay);

if (callNow) fn.apply(this, args);
};
}

const debouncedImageSelectionFormChange = debounce(async () => {
await imageSelectionFormChange();
}, 1000);

// Handle image selection modal
$("form#imageSelectionForm input").on("input", (e) => {
debouncedImageSelectionFormChange();
});
$("form#imageSelectionForm select").on("input", async (e) => {
await imageSelectionFormChange();
Expand Down

0 comments on commit 1ae5b2f

Please sign in to comment.