如何在超时时设置 Jquery 函数?

How to set a Jquery function on 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 );
                }

        } );

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

如果你只想在超时后执行,只需将代码包装在 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

});

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

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