From be70fd10d682fd8c45b5dbf77d0466c72f6c972d Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Wed, 6 Sep 2023 15:54:13 +0300 Subject: [PATCH] fixed prevent request in debounce stimulus controller --- .../behaviour/debounce_controller.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/app/javascript/controllers/behaviour/debounce_controller.js b/app/javascript/controllers/behaviour/debounce_controller.js index ef191e9..ab3c609 100644 --- a/app/javascript/controllers/behaviour/debounce_controller.js +++ b/app/javascript/controllers/behaviour/debounce_controller.js @@ -4,13 +4,22 @@ import { Controller } from "@hotwired/stimulus" export default class extends Controller { static targets = ["form"] + initialize() { + this.timeout = null; + this.initialLoad = true; + } + + search(event) { - event.preventDefault(); + if (this.initialLoad) { + this.initialLoad = false; + return; + } - clearTimeout(this.timeout) + clearTimeout(this.timeout); this.timeout = setTimeout(() => { - this.formTarget.requestSubmit() - }, 300) + this.formTarget.requestSubmit(); + }, 500); } disconnect() {