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

162
Visualizações
Ajax Requests Slow in Django

I have the following ajax request in my django template:

$('#subjects').on('change', function() {

    var subject = $('#subjects').find(":selected").text();

    $.ajax({
        type: "GET",
        url: "/classes/" + term + "/" + subject ,  // or just url: "/my-url/path/"
        dataType: "html",
        success: function(data) {
            $('#classes').html(data);
        }
    });

    //remove courses
    //remove overview

    //get courses for specified subject
    //put them under course
});

The "subject" id is for a select form like this:

<select size="7" class="form-control" id="subjects">
  {% for subject in subjects %}
    <option>{{ subject }}</option>
  {% endfor %}
</select>

So, when a subject is changed, I send an ajax request to the server so that I can get the classes for that subject from the database (as there are thousands of classes). However, this takes ~1 second. If a user simply arrows down my list of subjects, then tens of ajax requests will be fired off in a second, causing a backup and slowing down the data being displayed properly.

I tried aborting all previous ajax requests before sending another one, but the problem is the server will still process these, so it did not fix the problem.

Is there any way to speed this up, and somehow only take 1 second any time a user scrolls down to a subject? Or, is there another method that anyone would recommend?

Follow up question. Another way I just thought of would be to only send the ajax request if an option is selected for longer than 1 second. this would make it take 2 seconds which is fine. Is there a way to do this?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Do something like this enable/disable

$('#subjects').on('change', function() {

    var subject = $('#subjects').find(":selected").text();
    document.getElementById('subjects').disabled=true
    $.ajax({
        type: "GET",
        url: "/classes/" + term + "/" + subject ,  // or just url: "/my-url/path/"
        dataType: "html",
        success: function(data) {
            $('#classes').html(data);
            document.getElementById('subjects').disabled=false
        }
    });

 #rest of code
over 4 years ago · Santiago Trujillo Relatório

0

Answering to your follow up question, here is a jQuery function that allow to delay the callback of an event a given amount of milliseconds:

(function ($) {
    $.fn.delayOnEvent = function(onevent, callback, ms){
        $(this).on(onevent, function( event ){
            var srcEl = event.currentTarget;
            if( srcEl.delayTimer )
                clearTimeout ( srcEl.delayTimer );
            srcEl.delayTimer = setTimeout(function(){ callback( $(srcEl) ); }, ms);
        });
        return $(this);
    };
})(jQuery);

You can call it this way in your case:

$('#subjects').delayOnEvent('change', function() {
   ...
    }, 1000); // one second delay
over 4 years ago · Santiago Trujillo Relatório
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