1

実行を遅らせたいユーザー定義関数があります。fancy() を遅らせたいのですが、setTimeout を使用しています。しかし、それは瞬時に実行されます。別の時間遅延も設定しましたが、効果がありません。他の方法はありますか、それとも間違っていますか?? 助けてください。

前もって感謝します。アリ

$('a.vid').click(function(){


        setTimeout(fancy(this) ,2000 );


});



function fancy(that){

            var videoFile = $(that).attr('videofile');
            var videoWidth = Number($(that).attr('videowidth'));
            var videoHeight =Number( $(that).attr('videoheight'));

            var videoCode = '<video width="'+videoWidth+'" height="'+videoHeight+'" controls autoplay autobuffer><source src="includes/video/'+videoFile+'.ogv" type="video/ogg" /><source src="includes/video/'+videoFile+'.mp4" type="video/mp4" /><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+videoWidth+'" height="'+(videoHeight+40)+'" id="lynda_video_player" align="middle"><param name="allowScriptAccess" value="sameDomain"><param name="allowFullScreen" value="true"><param name="movie" value="lynda_video_player.swf?videoFile=includes/video/'+videoFile+'.mp4&amp;skinFile=lynda_video_skin.swf&amp;videoFileWidth='+videoWidth+'&amp;videoFileHeight='+videoHeight+'"><param name="quality" value="high"><param name="wmode" value="transparent"><param name="scale" value="noscale"><param name="salign" value="lt"><embed src="lynda_video_player.swf?videoFile=includes/video/'+videoFile+'.mp4&amp;skinFile=lynda_video_skin.swf&amp;videoFileWidth='+videoWidth+'&amp;videoFileHeight='+videoHeight+'" quality="high" width="'+videoWidth+'" height="'+(videoHeight+40)+'" name="lynda_video_player" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" scale="noscale" salign="lt" wmode="transparent" allowfullscreen="true" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object></video>';


            $('#videoPlayer').html(videoCode);

            $.fancybox({

                'transitionIn' : 'fade',
                'transitionOut' : 'fade',
                'overlayColor' : '#000',
                'overlayOpacity' : '.6',
                'href' : '#videoPlayer'


                });

    }
4

2 に答える 2

7

使用する

setTimeout(function() {
fancy(this);
}, 2000);
于 2013-04-07T08:18:43.827 に答える
1

匿名関数を setTimeout に渡す必要があるため、正しい形式は次のとおりです。

setTimeout(function() {fancy(this)}, delay);

ウェイ・トゥ・ウェイ

関数delayfunction() (パラメーターなし)がある場合、次は問題ありません

setTimeout(delayfunction, delay); //note  no `()`

関数にパラメーターを送信する場合は、目的の関数を呼び出す匿名関数を呼び出す必要があります。

setTimeout(function() {

    delayfunction('parms');

}, 2000);

重複: setTimeout を使用するとメソッドがすぐに実行されるのはなぜですか?

于 2013-04-07T08:19:21.263 に答える