views:

79

answers:

2

I am creating a background color animation with jQuery and the color animation plugin http://plugins.jquery.com/project/color

How do I have a function stay in a loop repeating itself each time it has finished running? I tried this but it did not work :

 function colorLoop(){
  $("#window")
     .animate({ backgroundColor: "orange" }, 11000)
  .animate({ backgroundColor: "violet" }, 1000)
  .animate({ backgroundColor: "red" }, 1000, colorLoop);
  };
A: 

You only created the function, but did not call it. Stick colorLoop(); between }; and });. Also, you really shouldn't be using jQuery for this kind of thing.

Delan Azabani
i tried this but it didnt work for me. Care to elaborate on why I shouldnt use jQuery?
zac
It depends on your audience context. Do you require IE support? If so, keep on with jQuery. If you don't, it's better for native standards development if you use native features and allow for graceful degradation on other browsers like IE. More about this on my blog post: http://azabani.com/51
Delan Azabani
Please tell me the error you are facing; I'd be happy to help.
Delan Azabani
its just a little bonus (changing background color), it wont affect usability at all if it doesnt work..
zac
i am not seeing any errors, it just runs through the colors once then stops
zac
Did you place `colorLoop();` *immediately before* the last three characters - `});`
Delan Azabani
yes that is how i have it
zac
I see the problem! If you want to put a callback, it must be the *fourth* argument, after a required *easing* string (default `swing`) as the *third* argument. You must explicitly define the easing in the last one: `.animate({backgroundColor: 'red'}, 1000, 'swing', colorLoop);`
Delan Azabani
interesting article btw, i totally agree, it is more i just wouldnt know how to pull this off without a library and i guess i would sadly say I do require IE support
zac
Ah, OK then. Does my last suggestion work (putting the 'swing' easing argument before colorLoop argument)?
Delan Azabani
no, it just makes it stop on violet in my example
zac
Please edit your post to put the current code you have right now, so I can help you better. It looks like you haven't added 'swing' as an argument before 'colorLoop'.
Delan Azabani
A: 

Use a loop, don't call a function, there's a good pulgin for Jquery:

Download Link

<script type= "text/javascript" src="jquery.timers-1.1.2.js" ></script>
    $("#window").everyTime(10, function(){
      $("#window")
        .animate({ backgroundColor: "orange" }, 11000)
        .animate({ backgroundColor: "violet" }, 1000)
        .animate({ backgroundColor: "red" }, 1000, 'swing');
      };
    });

This will be repeating the background animation...

Zuul
what is wrong with the native window.setInterval
redsquare
Nothing, it's pure Javascript and the solution I've presented uses Jquery (since your example was in Jquery) :)
Zuul
errr jquery is javascript, but there is no need to include 'another' plugin that simply wraps something so simple that is standard cross browser
redsquare
great! thanks redsquare that did the trick! i just added this :var intervalID=window.setInterval(colorLoop,500);
zac