import { Controller } from "@hotwired/stimulus"; // Connects to data-controller="hot-search" export default class extends Controller { connect() { this.debouncedSearch = debounce(this.debouncedSearch.bind(this), 400) } debouncedSearch() { Turbo.visit('/scores?q[name_cont]=' + this.element.value, { frame: 'scores', turbo: true, acceptsStreamResponse: true }) } } function debounce(callback, delay) { let timeoutId; return (...args) => { if (timeoutId) { clearTimeout(timeoutId) } timeoutId = setTimeout(() => { callback(...args) timeoutId = null }, delay) } }