views:

185

answers:

2

I'm trying to fade an element in and out, but with a slight pause in between, it works without the pause, but when I add the pause using the jQuery delay()-function, it just stops after the first fadeOut();

Here's the code:

$('#headerimage2').each(function(){
for(i=1;i<50;i++){
    $(this).fadeOut(1200).delay(1000).fadeIn(1000).delay(1000);
    }
});

Why does the delay()-function (both first and second) break the loop?

+3  A: 

Shot in the dark here, but are you sure you're using version 1.4 of the library. This is a new function as of that version.

BradBrening
I agree with @BradBrening, maybe you're not using jquery 1.4
DKinzer
Okay, I just saw it. Drupal 6.16 still has 1.3.2, the update module is aleady in alpha state though. Can I do it with setTimeOut as well? Or just paste the entire new Jquery Libary in the drupal files myself perhaps?
Rakward
I'm not famliar with Drupal, but if you're stuck with 1.3.2 of the jQuery library, you can use the callback parameter of the fadeOut function to define a function that then calls setTimeOut with fadeIn as its callback. A bit of a chain of events, but it should work.
BradBrening
+1  A: 

Your code as posted works perfectly in firefox, safari, and chrome with the latest jquery:

<!DOCTYPE html>
<html>
<head>
  <style>
div { width: 60px; height: 60px; float: left; }
.first { background-color: #3f3; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
</head>
<body>

<p><button>Run</button></p>
<div id='headerimage2' class="first"></div>
<script>
    $("button").click(function() {
    $('#headerimage2').each(function(){
    for(i=1;i<5;i++){
        $(this).fadeOut(100).delay(500).fadeIn(100).delay(500);
       }
    });
    });
</script>
</body>
</html>
Bosh