Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
Autocomplete not showing current suggestions

Flask

@app.route("/get_title", methods = ["POST"])
def send_title():
  data = request.get_json()
  qry = Category.query.filter(Category.book_category == data['category']).first()
  qry = [(i.bookname) for i in qry.booklist.filter(Books.bookname.like(data["search"])).limit(10)]
  return json.dumps(qry)

JavaScript


async function loadNames(selectval, inputval) {
  var your_data = {
    "category": selectval,
    "search": `%${inputval}%`
  }
  const response = await fetch(`/get_title`, {
    method: "POST",
    credentials: "include",
    body: JSON.stringify(your_data),
    cache: "no-cache",
    headers: new Headers({
      "content-type": "application/json"
    })
  })
  const names = await response.json();
  return names
}

let timeout = null
function debounce(func, wait, immediate) {
    var timeout;
    return function() {
        var context = this, args = arguments;
        var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
};

var debounce_key = debounce(async function(){
  var selectval = $('select').val()
  var inputval = $('#Title').val()
  let data = await loadNames(selectval, inputval)
    
    $('#Title').autocomplete({
      source: data
    })
}, 350)

$("#Title").on("keyup", debounce_key)


Everytime a character is typed there would be a 0.35 seconds countdown and the fetch to the Flask route happens (returning related bookname)

However, in instance like this:

A user typed a whole word without reaching the 0.35s countdown > fetched /get_title > json received in javascript file > no auto suggest happens!!

The only way to trigger the autosuggest is to type another word (which would use the last fetched data as basis on autosuggest)

How could I make it as the user stops > 0.35 s countdown triggered > fetch > json received > autosuggest shows based on the current input.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda