Files
easy-going-rails/app/javascript/controllers/hot_search_controller.js
T
2024-09-05 13:22:20 +04:00

23 lines
609 B
JavaScript

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)
}
}