-3

I want to make the div fadeIn and fadeOut after every 2 seconds, but the setTimeout function is running only once and hiding the div, but that is it. It is not doing it more than once:

HTML:

<a href="javascript:void(0);">Pyp1</a>
<div id="disable">
    Hello!
</div>

JavaScript (jQuery):

$(document).ready(function(){
    setTimeout(function() {
        $('#disable').toggle('fast');
    }, 2000);
});

Fiddle

However, when I write the same thing for the click event, it actually works:

$(document).ready(function(){
    $('a').click(function() {
        $('#disable').fadeToggle('fast')
    });
});

Fiddle

I've tried searching around (in Stack Overflow as well) and couldn't get the answer, I tried the click approach and it worked, but that is not the result I am looking for.

Where exactly is the problem and how may I fix it?

Braiam
  • 1
  • 11
  • 47
  • 78

1 Answers1

1

This is because setTimeout runs only once, how can you expect it to run multiple times? Use setInterval instead.