Reputation: 85
I want to find a way to set a timeout after each character typed by user so it will not make a request to algolia server after each character typed.
so when user types a character it waits 1 second, and if user does not type any other key then send the request to algolia server
something like this:
function onUserKeyUp (){
clearTimeout('TIMEout');
TIMEout=setTimeout(function() {
SendRequestToALGOLIA()
}, 1000);
}
do algolia has api for this?
Upvotes: 0
Views: 182
Reputation: 3177
Before anything, note that Algolia sends request at every keystroke for User-experience reasons: instant-search experiences are most engaging for users, while delaying network requests on user interaction will make visitors feel like the interface is stuttering, resulting in a suboptimal experience for them.
This being said, although Algolia doesn't recommend it, you can build this using their API Clients and setTimeout
.
Read an example usage of the API Client you want to use. For example with Algolia's Places API: Example using an API Client
sendRequestToAlgolia()
Define a setAlgoliaTimeout()
function that uses setTimeout
and clearTimeout
as documented on this W3Schools example to trigger that sendRequestToAlgolia()
function after a delay of your choice
Follow MDN's example for keyup event to define a listener that triggers your setAlgoliaTimeout()
method when a valid keyup
events arrives.
Upvotes: 0