Reputation: 280
I have this code:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
function loop(){
$('#picOne').fadeIn(0).fadeOut(8000);
$('#picTwo').delay(2000).fadeIn(6000).fadeOut(5000);
$('#picTree').delay(10000).fadeIn(2000).fadeOut(16000);
$('#picFour').delay(12000).fadeIn(16000).fadeOut(5000);
}
loop();
});
</script>
But when the last pic fades out, the code doesn't repeat. What is the problem?
Upvotes: 1
Views: 33453
Reputation: 178
setInterval only except function try this
function loop(){
$('#picOne').fadeIn(0).fadeOut(8000);
$('#picTwo').delay(2000).fadeIn(6000).fadeOut(5000);
$('#picTree').delay(10000).fadeIn(2000).fadeOut(16000);
$('#picFour').delay(12000).fadeIn(16000).fadeOut(5000);
}
setInterval(loop, 5000);
Upvotes: 1
Reputation: 1
I would like to say that this function does work well, and whomever did it, did a great job. I edited the demo and it works well with pictures.
<div id="picOne">
<img id="picOne" src="http://www.phphq.net/demos/phAlbum/album/Windows%20Wallpapers/Missing/Waterfall.jpg"/></div>
<div id="picTwo">
<img id="picTwo" src="http://newevolutiondesigns.com/images/freebies/colorful-background-21.jpg"/></div>
<div id="picTree">
<img id="picTree" src="http://www.phphq.net/demos/phAlbum/album/Windows%20Wallpapers/Missing/Waterfall.jpg"/></div>
<div id="picFour">
<img id="picFour" src="http://www.photoinpixel.com/mypicture/amazing-background-wallpapers.jpg"/></div>
Function
var $elements = $('#picOne, #picTwo, #picTree, #picFour');
function anim_loop(index) {
$elements.eq(index).fadeIn(1000, function() {
var $self = $(this);
setTimeout(function() {
$self.fadeOut(1000);
anim_loop((index + 1) % $elements.length);
}, 3000);
}); }
anim_loop(0); // start with the first element
Upvotes: 0
Reputation: 816404
Assuming you want to duration of the animation being the same for each element:
var $elements = $('#picOne, #picTwo, #picTree, #picFour');
function anim_loop(index) {
// Get the element with that index and do the animation
$elements.eq(index).fadeIn(1000).delay(3000).fadeOut(1000, function() {
// Kind of recursive call, increasing the index and keeping in the
// the range of valid indexes
anim_loop((index + 1) % $elements.length);
});
}
anim_loop(0); // start with the first element
I don't know exactly how the animation should be, but I hope it makes the concept clear.
Update: To simultaneously fade out and in an image after a certain time period, use setTimeout
and call fadeOut
and anim_loop
in the callback:
$elements.eq(index).fadeIn(1000, function() {
var $self = $(this);
setTimeout(function() {
$self.fadeOut(1000);
anim_loop((index + 1) % $elements.length);
}, 3000);
});
Upvotes: 7
Reputation: 7445
There are no problem couse your function is called only once.
If you want to looped them you can use setInterval()
or setTimeout()
setInterval(function(){loop()}, 16000);
function loop(){
$('#picOne').fadeIn(0).fadeOut(8000);
$('#picTwo').delay(2000).fadeIn(6000).fadeOut(5000);
$('#picTree').delay(10000).fadeIn(2000).fadeOut(16000);
$('#picFour').delay(12000).fadeIn(16000).fadeOut(5000);
}
or
function loop(){
$('#picOne').fadeIn(0).fadeOut(8000);
$('#picTwo').delay(2000).fadeIn(6000).fadeOut(5000);
$('#picTree').delay(10000).fadeIn(2000).fadeOut(16000);
$('#picFour').delay(12000).fadeIn(16000).fadeOut(5000);
setTimeout(function(){loop()}, 16000);
}
In both cases function will be called every 16 seconds = 16000 miliseconds
.
Upvotes: 0