...

View Full Version : Delay script from starting



Ant-B
03-12-2012, 11:28 AM
Hey i need some help delaying this script so it starts 5 seconds after the page loads not instantly.

:)



<script>


$('#ad_1 > img').each(function(i,e){
rotate($(this),600,4000,i);
});
function rotate(elem1,speed,timeout,i){
elem1.animate({'marginLeft':'18px','width':'0px'},speed,function(){
var other;
if(elem1.parent().attr('id') == 'ad_1')
other = $('#ad_2').children('img').eq(i);
else if(elem1.parent().attr('id') == 'ad_2')
other = $('#ad_3').children('img').eq(i);
else if(elem1.parent().attr('id') == 'ad_3')

other = $('#ad_1').children('img').eq(i);

other.animate({'marginLeft':'0px','width':'35px'},speed,


function(){
var f = function() { rotate(other,speed,timeout,i) };
setTimeout(f,timeout);
});
});

}

});


</script>

webdev1958
03-12-2012, 01:33 PM
Hey i need some help delaying this script so it starts 5 seconds after the page loads not instantly
In your window onload event handler you can use setTimeout() (http://www.w3schools.com/jsref/met_win_settimeout.asp) to delay the execution of some code.

Ant-B
03-12-2012, 02:00 PM
In your window onload event handler you can use setTimeout() (http://www.w3schools.com/jsref/met_win_settimeout.asp) to delay the execution of some code.

that sounds like what i need can you show me an example please?

webdev1958
03-12-2012, 02:15 PM
what don't you understand about the example in the link I posted?

Ant-B
03-12-2012, 02:30 PM
what don't you understand about the example in the link I posted?

oh sorry i did not notice the link :O

Ant-B
03-12-2012, 02:37 PM
Could i place a setTimeout before the code to cause a delay in it running?

Ant-B
03-12-2012, 02:50 PM
could i also place a .delay(5000) in there somewhere? to give a 5 second gap before the start?

webdev1958
03-13-2012, 12:56 AM
Hey i need some help delaying this script so it starts 5 seconds after the page loads not instantly.



Could i place a setTimeout before the code to cause a delay in it running?

Like I said earlier - put it in your window onload event handler.

DaveyErwin
03-13-2012, 01:33 AM
Hey i need some help delaying this script so it starts 5 seconds after the page loads not instantly.

:)




<script>


$('#ad_1 > img').each(function(i,e){
rotate($(this),600,4000,i);
});
function rotate(elem1,speed,timeout,i){
elem1.animate({'marginLeft':'18px','width':'0px'},speed,function(){
var other;
if(elem1.parent().attr('id') == 'ad_1')
other = $('#ad_2').children('img').eq(i);
else if(elem1.parent().attr('id') == 'ad_2')
other = $('#ad_3').children('img').eq(i);
else if(elem1.parent().attr('id') == 'ad_3')

other = $('#ad_1').children('img').eq(i);

other.animate({'marginLeft':'0px','width':'35px'},speed,


function(){
var f = function() { rotate(other,speed,timeout,i) };
setTimeout(f,timeout);
});
});

}

});

</script>

<script>
function delayed(){
$('#ad_1 > img').each(function(i,e){
rotate($(this),600,4000,i);
});
function rotate(elem1,speed,timeout,i){
elem1.animate({'marginLeft':'18px','width':'0px'},speed,function(){
var other;
if(elem1.parent().attr('id') == 'ad_1')
other = $('#ad_2').children('img').eq(i);
else if(elem1.parent().attr('id') == 'ad_2')
other = $('#ad_3').children('img').eq(i);
else if(elem1.parent().attr('id') == 'ad_3')

other = $('#ad_1').children('img').eq(i);

other.animate({'marginLeft':'0px','width':'35px'},speed,


function(){
var f = function() { rotate(other,speed,timeout,i) };
setTimeout(f,timeout);
});
});

}

});
};
</script>
<body onload="setTimeout(delayed,5000)">

webdev1958
03-13-2012, 01:49 AM
<body onload="setTimeout(delayed,5000)">

The risk with attaching the onload to the body is that images that are being preloaded (which is what I would normally do in this situation) might still be downloading when the body onload is triggered.

I'm pretty sure that since images are downloaded in a parallel stream to the rest of the page, the window onload is triggered after the images have finished downloading as well.

The last thing you want is for the animation to start before all the images are downloaded.

Hence my suggestion to put the setTimeout() in the window's onload event handler.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum