我有一个用户定义的功能,我想延迟它的执行。想要 延迟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&skinFile=lynda_video_skin.swf&videoFileWidth='+videoWidth+'&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&skinFile=lynda_video_skin.swf&videoFileWidth='+videoWidth+'&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 :(得分:7)
使用
setTimeout(function() {
fancy(this);
}, 2000);
答案 1 :(得分:1)
你必须将一个匿名函数传递给setTimeout,所以正确的形式是:
setTimeout(function() {fancy(this)}, delay);
方式
如果你有一个功能延迟功能()(带out参数)以下就可以了
setTimeout(delayfunction, delay); //note no `()`
如果你想向这个函数发送参数,你必须调用一个匿名函数,然后调用你想要的函数。
setTimeout(function() {
delayfunction('parms');
}, 2000);
重复:Why is the method executed immediately when I use setTimeout?