Skip to content
Advertisement

Django – AJAX Fetch sends request for each input letter

This might be far from how it should be done, I’m learning on the go and it’s my first time trying something like this.

Problem: Even with the setTimeout function, server sends response for each letter I have written, though I would expect it to wait for user to stop typing and just fetch the finished word(s)

Script in my template:

JavaScript

views.py

JavaScript

Advertisement

Answer

Store timeout id to variable in the scope higher than yours event listner. When event fires up – check if there was a timeout and clear it (which means cancel the request if it wasn’t yet executed)

Example:

JavaScript
Advertisement