Display a div for a given time Jquery

Asked

Viewed 438 times

0

I wonder if there’s any way I can present the contents of a div at a certain time. Because in this case I’m inserting an html into the div and after a while I’d like it to disappear, there’s some way to do it?

    <div id="alerta">

    </div>

@if (cadastradoComSucesso)
{
    <script>
        var functionSuccess = function () {
            $('#alerta').html('<div class="alert alert-success" role="alert"> Registro cadastrado com sucesso.</div>')
        };        
        setTimeout(functionSuccess, 8000);     
    </script>
}
else
{
    <script>
        var functionWarrning = function () {
            $('#alerta').html('<div class="alert alert-warning" role="alert"> Erro ao salvar registro.</div>')
        };
        setTimeout(functionWarrning, 8000);        
    </script>

}
  • What do you have inside your job? You tried $().hide jQuery?

  • So, but in this case, what would I call Hide? I would have to have a timer for that not?

  • Your setTimeOut doesn’t do that anymore?

  • I honestly don’t know how the question was positive, it shows no research effort, since it’s something you should have about 8000 duplicates here in the Stack... a quick googlada already gives the answer.

2 answers

1


You can use setTimeout and then give the Hide in div.

setTimeout(function(){
   //Esconde a div
   $('#alerta').hide();
}, seu_tempo_em_millisegundos);

0

I got it that way:

<script>
    var functionWarrning = function () {
        $('#alerta').html('<div class="alert alert-warning" role="alert"> Erro ao salvar registro.</div>')
        $('#alerta').show();
    };

    functionWarrning();

    setTimeout(function () {
        $('#alerta').hide();
    }, 8000);         
</script>   
  • But then the div will still continue with the content, even hidden.

  • In this case for me it is not a problem, because as I am putting the content with . html and not append, it subistitu everything inside the div.

  • Strange. You ask the question, a lot of people answer and then almost at the same time you answer your own question with the same solution as the other answers.

  • @DVD I just complemented how I managed to solve the problem, and of course, by joining all the help given by our friends. Sometimes it can help someone in the future, knowing how I actually solved the problem

  • 1

    So it’s not an answer, it’s just an issue of the question, since it marked another answer as "correct".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.