【问题标题】:Using setTimeout to delay timing of jQuery actions使用 setTimeout 来延迟 jQuery 动作的时间
【发布时间】:2013-06-05 17:02:44
【问题描述】:

我正在尝试延迟 div 中文本的交换。它应该像文本的滑块/轮播一样操作。

我一定是代码错了,因为最终的文本替换永远不会发生。

另外,我将如何动画介绍替换文本(例如百叶窗)?


<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />

        <script type="text/javascript">
$(document).ready(function() {

    $("#showDiv").click(function() {
        $('#theDiv').show(1000, function() {
            setTimeout(function() {
                $('#theDiv').html('Here is some replacement text', function() {
                    setTimeout(function() {
                        $('#theDiv').html('More replacement text goes here');
                    }, 2500);
                });
            }, 2500);
        });
    }); //click function ends

}); //END $(document).ready()

        </script>
    </head>
<body>

    Below me is a DIV called "theDiv".<br><br>
    <div id="theDiv" style="background-color:yellow;display:none;width:30%;margin:0 auto;">
        This text is inside the Div called "theDiv".
    </div><br>
    <br>
    <input type="button" id="showDiv" value="Show DIV">



</body>
</html>

【问题讨论】:

  • 我认为最合适的工具是.queue()...

标签: javascript jquery


【解决方案1】:

.html() 只接受一个字符串或一个函数作为参数,not both。试试这个:

 $("#showDiv").click(function () {
     $('#theDiv').show(1000, function () {
         setTimeout(function () {
             $('#theDiv').html(function () {
                 setTimeout(function () {
                     $('#theDiv').html('Here is some replacement text');
                 }, 0);
                 setTimeout(function () {
                     $('#theDiv').html('More replacement text goes here');
                 }, 2500);
             });
         }, 2500);
     });
 }); //click function ends

jsFiddle example

【讨论】:

  • 如果我把 放在那个 div 里面会怎么样。脚本也会被刷新吗?什么的。
【解决方案2】:

试试这个:

function explode(){
  alert("Boom!");
}
setTimeout(explode, 2000);

【讨论】:

    【解决方案3】:

    您也可以使用jQuery's delay() method 代替setTimeout()。它会给你更多可读的代码。这是文档中的一个示例:

    $( "#foo" ).slideUp( 300 ).delay( 800 ).fadeIn( 400 );
    

    唯一的限制(据我所知)是它无法为您提供清除超时的方法。如果您需要这样做,那么您最好坚持使用 setTimeout 强加给您的所有嵌套回调。

    【讨论】:

    • 其实你可以使用clearQueue()来移除任何还没有执行的动画。
    【解决方案4】:

    这就是我解决问题的方法 鼠标移出几秒钟后菜单关闭(如果悬停没有触发),

    //Set timer switch
    $setM_swith=0;
    
    $(function(){
        $(".navbar-nav li a").click(function(event) {
            if (!$(this).parent().hasClass('dropdown'))
                $(".navbar-collapse").collapse('hide');
        });
        $(".navbar-collapse").mouseleave(function(){
            $setM_swith=1;
            setTimeout(function(){ 
               if($setM_swith==1) {
                 $(".navbar-collapse").collapse('hide');
                 $setM_swith=0;} 
            }, 3000);
        });
        $(".navbar-collapse").mouseover(function() {
            $setM_swith=0;
        });
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 2013-08-18
      • 2011-02-12
      • 2018-01-23
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      相关资源
      最近更新 更多