Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide for 10 seconds

I'm doing a small script which the element should disappear for only 2 seconds and then return to appear alone, I leave the link here Fiddle

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $('img').click(function() {
                $(this).fadeOut();
                setTimeout(function() {
                    $(this).fadeIn();

                }, 1000);
                //$(this).toggle();
            });
        });
    </script>

im trying using fade and set time but it not works

like image 710
Nicole Avatar asked Jan 29 '23 20:01

Nicole


1 Answers

The this on $(this).fadeIn(); does not point to the image anymore.

Try this:

 $(document).ready(function() {
            $('img').click(function() {
                var img = this; /* Store it on variable img */
                $(img).fadeOut();
                setTimeout(function() {
                    $(img).fadeIn(); /* Can access variable img here */
                }, 1000);
                //$(this).toggle();
            });
        });

Fiddle: https://jsfiddle.net/7rfypomx/1/

like image 77
Eddie Avatar answered Feb 23 '23 06:02

Eddie