2013-04-07 163 views
1

我有一个用户定义的函数,我想延迟它的执行。想要 延迟花式(),我正在使用setTimeout。但它瞬间运行。我也设置了不同的时间延迟 但它不起作用。是否有任何其他方法或我使用它错了?请帮忙。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' 


       }); 

    } 
+0

+1欢迎到我们的社会! 2小时内向我们的社区提出2个问题! – 2013-04-07 08:29:56

+0

谢谢。你们是非常有帮助的。 – user2253925 2013-04-07 09:08:55

+0

[为什么我使用setTimeout时立即执行该方法?](http://stackoverflow.com/questions/7137401/why-is-the-method-executed-immediately-when-i-use-settimeout) – 2013-04-07 13:07:27

回答

7

使用

setTimeout(function() { 
fancy(this); 
}, 2000); 
+0

延迟工作,但fancybox无法捕捉延迟前工作的视频。 – user2253925 2013-04-07 09:16:18

1

你必须通过一个匿名函数setTimeout的,所以正确的方式是:

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

途径路

如果你有一个功能delayfunction()(带OUT参数)以下是确定

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

如果要发送参数的功能,你将不得不调用一个匿名函数然后将调用您所需的功能。

setTimeout(function() { 

    delayfunction('parms'); 

}, 2000); 

重复:Why is the method executed immediately when I use setTimeout?

+0

延迟工作。 fancybox弹出框在延迟时间后打开,但没有视频。我也试过setTimeout(function(){ // function body },2000);用$(this)而不是$(that)。 – user2253925 2013-04-07 09:03:57