Start Jquery function when a div receives a class

0

I have a div class="modal-load" with a counter loader that will be shown when the user finishes a payment, but I need this counter to start only when < .active in it. For this, I created a function in JS that runs this load. I believe this function should be run only after receiving the informed class.

I tested it as follows:

$(document).ready(function() {
    if ($(".modal-load").hasClass('ativo')) {
        startLoader();
    }
});

When I leave the class already inline and refresh the page works perfectly. However, when I leave to add the class by the console, nothing done!

    
asked by anonymous 02.08.2018 / 20:39

1 answer

0
$(document).ready(function() {
    $(".modal-load").addClass("ativo");   //adiciona a classe ativo

    if ($(".modal-load").hasClass('ativo')) {  //se foi adicionada executa o loader
        startLoader();
        console.log('Sucesso!');
    } else {
        console.log("Não tem classe ativo");
    }
});
    
02.08.2018 / 20:58