user2253925
user2253925

Reputation: 25

setTimeout not working for delay

I have a user defined function i want to delay it's execution. want to delay fancy() and I am using setTimeout. But it runs instantaneously.I have also set different time delays but it does not effect.Is there any other method or I am using it wrong?? please help.

Thanks in advance. Ali

$('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'


                });

    }

Upvotes: 1

Views: 5851

Answers (2)

internals-in
internals-in

Reputation: 5038

you have to pass an anonymous function to setTimeout, so the correct form is:

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

Way to Way

if you have a function delayfunction() (with out parameter) following is ok

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

If you want to send parameters to the function, you will have to call an anonymous function which will then call your desired function.

setTimeout(function() {

    delayfunction('parms');

}, 2000);

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

Upvotes: 1

Elger Mensonides
Elger Mensonides

Reputation: 7029

use

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

Upvotes: 7

Related Questions