Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why setTimeout() only run my code once,at first time? [duplicate]

i use this javascript code to open two pictures and toggle a vertical menu by clicking on another picture. an know i want to run code without clicking on image, with a timer. so i wrote this code but it run only once at first time. what's wrong with my code?

<script type="text/javascript">
        $(document).ready(function () {
            $("#lista2").slideToggle(1);
            $curtainopen = false;
                        $(".rope").click(function () {
            $(this).blur();
            if ($curtainopen == false) {
                var selected = $(this).val();
                var image = $(".rope");
                image.fadeOut('fast', function () {
                    $("#largeImg").attr('src', 'images/power-on.png');
                    image.fadeIn('fast');
                });

                $(".leftcurtain").stop().animate({ left: '-120px' }, 2000);
                $(".rightcurtain").stop().animate({ left: '120px' }, 2000);

                $("#R").attr('src', 'images/Right.gif');
                $("#L").attr('src', 'images/Left.gif');
                $curtainopen = true;
                $("#lista2").slideToggle(2000);
                $(this).attr('id', '1');
            } else {
                var selected = $(this).val();
                var image = $(".rope");
                image.fadeOut('fast', function () {
                    $("#largeImg").attr('src', 'images/power-off.png');
                    image.fadeIn('fast');
                });

                $(".leftcurtain").stop().animate({ left: '0px' }, 2000);
                $(".rightcurtain").stop().animate({ left: '0px' }, 2000);
                $curtainopen = false;
                $("#lista2").hide();
                $(this).attr('id', '0');
            }
            return false;
                        });
        });

            function startTimer() {
                setTimeout($(".rope").click(), 4000);

            }

    </script>

like image 684
user3160084 Avatar asked Aug 14 '26 19:08

user3160084


2 Answers

use this to execute your code after a specific time interval

setInterval(function() {
    $(".rope").click(); // this will execute after every 4 sec.
}, 4000);

use this to execute your code after a specific time delay

 setTimeout(function() {
        $(".rope").click(); // this will execute after 4 sec delay only once.
    }, 4000);

use above according to your requirement

like image 97
Satish Sharma Avatar answered Aug 16 '26 07:08

Satish Sharma


setTimeout need a function, When you are passing $(".rope").click() it is called immediately.

Use it like

function startTimer() {
    setTimeout(function () {
        $(".rope").click();
    }, 4000);
}
like image 36
Satpal Avatar answered Aug 16 '26 09:08

Satpal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!