0

I know this has been asked several times but I just don't get it. So here's the case:

$ = jQuery;
    var loop = function() {
    $('#content').delay(800).css('background-color', "#B24296");
    $('#content').delay(1600).css('background-color', "#AEB404");
    $('#content').delay(2400).css('background-color', "#04B404");
    loop();
}
$(document).ready(function() {
    loop();

This setup gives me an "Uncaught RangeError: Maximum call stack size exceeded" I also tried this:

var loop = function() {
    $('#content').delay(800).css('background-color', "#B24296", function() {
        $('#content').delay(1600).css('background-color', "#AEB404", function() {
            $('#content').delay(2400).css('background-color', "#04B404");
        });
    });
}
$(document).ready(function() {
    setInterval(loop,3200);

With this code only the first color change happens but the rest of it won't. So unfortunately none of this does the job of simply changing the background-color from time to time ... Does anyone know a solution or can explain why non of this works?

Edit: Managed to get it to work like this:

function color(t) {
    $("div").delay(t*1).queue(function(n) {
         $('#green').css('background-color', "#B24296");
        n();
    });

    $("div").delay(t*2).queue(function(n) {
         $('#green').css('background-color', "#AEB404");        n();
    });

    $("div").delay(t*3).queue(function(n) {
         $('#green').css('background-color', "#04B404");
        n();
    });
    setTimeout(function() {
       color(500);
    }, 500);
}

color(500);
3
  • First one is an infinite loop. Commented Oct 28, 2013 at 21:50
  • Tail recursion: en.wikipedia.org/wiki/Tail_recursion Commented Oct 28, 2013 at 21:53
  • 2
    you also cannot perform .delay() on .css()... it just jumps to the end Commented Oct 28, 2013 at 21:54

1 Answer 1

2

In your first attempt, the function loop() calls itself an infinite amount of times resulting in the error you reported.

As for why the css/delay combination doesn't work, as per this answer: Using jQuery delay() with css() delay() works with the animation fx queue and has no effect on a css() call.

Again per the solution in Using jQuery delay() with css(), here is working code for your use case (http://jsfiddle.net/mmSVF/3/):

var loop = function() {
  $('#content').delay(800).queue(function(next){
      $(this).css('background-color', "#B24296");
      next();
  }).delay(1600).queue(function(next){
      $(this).css('background-color', "#AEB404");
      next();
  }).delay(2400).queue(function(next){
      $(this).css('background-color', "#04B404");
      setTimeout(loop, 500);
      next();
  });
}

$(function(){
    loop(); 
});
Sign up to request clarification or add additional context in comments.

3 Comments

First of all, thank you. Second, this changes the color three times but just once, but i actually want it to change over and over again like an invinite loop, but without causing the error
You can add setTimeout(loop, 500); before the 3rd call to next() to achieve this result. I'll make this edit to the code in my answer.
Thanks you very much! This is the first solution I understand!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.