【问题标题】:why setTimeout() only run my code once,at first time? [duplicate]为什么 setTimeout() 第一次只运行一次我的代码? [复制]
【发布时间】:2014-01-04 11:23:04
【问题描述】:

我使用此 javascript 代码打开两张图片并通过单击另一张图片来切换垂直菜单。知道我想在不单击图像的情况下使用计时器运行代码。所以我写了这段代码,但它第一次只运行一次。 我的代码有什么问题?

<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>

【问题讨论】:

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

标签: javascript jquery


【解决方案1】:

使用它在特定时间间隔后执行您的代码

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

使用它在特定时间延迟后执行您的代码

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

根据您的要求使用以上

【讨论】:

    【解决方案2】:

    setTimeout 需要一个函数,当你传递$(".rope").click() 时,它会立即被调用。

    像这样使用它

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

    【讨论】:

    • 请在回答时考虑解释问题。
    【解决方案3】:
    setTimeout(function() {
        $(".rope").click();
    }, 4000);
    

    因为setTimeout 需要一个函数,但$(".rope").click() 会立即调用自身(而不是分配要调用的函数)。所以你不想调用一个函数,而是将它传递给setTimeout。

    【讨论】:

      【解决方案4】:

      计时器意味着在每次超时后重复该功能。 setTimeOut 仅将函数延迟一次(在给定时间之后,以毫秒为单位)。

      function startTimer() {
        //do your stuff
        $(".rope").click();
        //repeats itself after 4 seconds
        setTimeout(startTimer, 4000);
      }
      

      别忘了在文件准备好时启动它:

      $(document).ready(function () {
        startTimer();
        ...
      }
      

      如果您不希望在页面加载时立即调用您的函数,您可以添加初始延迟:

      $(document).ready(function () {
        setTimeout(startTimer, 5000); //the timer will start only 5 seconds after page load
        ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        • 2017-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多