【问题标题】:How to set a Jquery function on timeout?如何在超时时设置 Jquery 函数?
【发布时间】:2016-03-15 03:06:42
【问题描述】:

我找到了如何在 timeout 上设置函数。但它在这个 JQUERY 函数中不起作用。请帮助我。这是jquery代码:

$(function() {          
            var $wrapper= $( '#fc-wrapper' ),
                $handle = $wrapper.children( 'div.fc-handle-pull' );                

            $(function( event ) {               
                ( $handle.data( 'opened' ) ) ? close() : open();                
            } );

            $wrapper.hammer().bind( 'dragend', function( event ) {
                switch( event.direction ) {
                    case 'right': open(); break;
                    case 'left': close(); break;
                }
            });

            function open() {
                $wrapper.addClass( 'fc-wrapper-open' );
                $handle.data( 'opened', true );
            }

            function close() {
                $wrapper.removeClass( 'fc-wrapper-open' );
                $handle.data( 'opened', false );
                }

        } );

请告诉我如何在超时时设置此功能。提前致谢

【问题讨论】:

  • 你能告诉我们你的尝试吗?
  • $(function( event ) { ( $handle.data 让 jQuery 包装它是没有意义的。
  • 你想要什么超时???
  • 它使用了一个名为hammer.js 的jquery 插件。所以这是有道理的。我只想知道将所有内容设置为 timeout 。谢谢
  • 我认为您应该提供更多信息。有链接或截图吗?

标签: jquery function timeout


【解决方案1】:

我自己解决了这个问题。这是代码:

setTimeout( function(){ 

            var $wrapper= $( '#fc-wrapper' ),
                $handle = $wrapper.children( 'div.fc-handle-pull' );                

            $(function( event ) {               
                ( $handle.data( 'opened' ) ) ? close() : open();                
            } );

            $wrapper.hammer().bind( 'dragend', function( event ) {
                switch( event.direction ) {
                    case 'right': open(); break;
                    case 'left': close(); break;
                }
            });

            function open() {
                $wrapper.addClass( 'fc-wrapper-open' );
                $handle.data( 'opened', true );
            }

            function close() {
                $wrapper.removeClass( 'fc-wrapper-open' );
                $handle.data( 'opened', false );
                }


        }  , 2000 );

【讨论】:

    【解决方案2】:

    如果你只是想让它在超时后执行,只需将代码包装在setTimeout

    $(function() {
      // call setTimeout with a function and a timeout length in milliseconds
      var waitLength = 1000; // ms
      var timer = setTimeout(function () {
    
            var $wrapper= $( '#fc-wrapper' ),
                $handle = $wrapper.children( 'div.fc-handle-pull' );                
    
            // no need to wrap this in $(function(){...}) since you already
            // did that on line 1
            $handle.data( 'opened' ) ? close() : open();
    
            $wrapper.hammer().bind( 'dragend', function( event ) {
                switch( event.direction ) {
                    case 'right': open(); break;
                    case 'left': close(); break;
                }
            });
    
            function open() {
                $wrapper.addClass( 'fc-wrapper-open' );
                $handle.data( 'opened', true );
            }
    
            function close() {
                $wrapper.removeClass( 'fc-wrapper-open' );
                $handle.data( 'opened', false );
            }
    
      }, waitLength); // end setTimeout call
    
    });
    

    【讨论】:

      猜你喜欢
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多