0

I have a border 9x9, with lines, columns and squares, like a sudoku border and I want to animate it but I had a few problems when there was multiple animations at same time, so I decided to animate one array of elements each time, for that I putted them in an array and want to animate it one by one but I'm having some issues. There is a piece of my code:

var col,
        row,
        square,
        arr = [];

    col = checkColumn(cell);
    row = checkRow(cell);
    square = checkSquare(cell);

    if(row != null){
        arr.push(row);
    }

    if(col != null){
        arr.push(col);
    }

    if(square != null){
        arr.push(square)
    }

    if(arr.length !=0){
        arr.each(function(){
            enlight($(this));
        });
    }

So, at the moment I can't use arr.each because it says arr isnt a function, If I change arr to:

arr ={}

I can't use .push to add the elements, is there an solution to animate them one by one? Thank you in advance for the help!

The enlight does this:

function enlight(cells){
    var delayTime=0;
    cells.each(function(){
        $(this).parent().delay(delayTime).animate({
            "background-color" : "#ffa500"

        }, 500).delay(100)
            .animate({
                "background-color" : "#ffffff"
            });
        delayTime+=100;
    })

}

So, I want to sent an array each time (row, col and square) to animate one by one not all at same time.

Some HTML:

<div class="board">
      <div class="row">
        <div class="cell">
            <input type="number"  data-column="0" data-line="0">
        </div>
        <div class="cell">
            <input type="number"  data-column="1" data-line="0">
        </div>
        <div class="cell">
            <input type="number"  data-column="2" data-line="0">
        </div>
        <div class="cell">
            <input type="number"  data-column="3" data-line="0">
        </div>
        (more cells 81 in total)
17
  • Animate what, it's an array and a number of unspecified functions? Commented Oct 24, 2016 at 17:28
  • col, row and square are arrays of 9 cells each, the enlight function works well but I just want to pass here one of them at a time. Commented Oct 24, 2016 at 17:29
  • You are passing them one at a time, but the animation is probably async, so you have to wait for something, but as that code isn't posted, there's no way to know what to wait for, or for how long ? Commented Oct 24, 2016 at 17:31
  • My problem here isn't, the problem is with the var arr, if I let it arr = [] I cant use the function arr.each(). If I make it arr = {} I can't use the function .push() Commented Oct 24, 2016 at 17:33
  • 1
    Maybe I'm just complicating this for you, here's another example -> jsfiddle.net/h8ge7s89/6 Commented Oct 24, 2016 at 18:46

2 Answers 2

1

so I decided to animate one array of elements each time, for that I putted them in an array and want to animate it one by one

You can use .queue(), .promise(), .then(), $.map() to animate elements in sequential order.

// set queue name for `.row` element to `"enlighten"` iterate `.row` elements
$({}).queue("enlighten", $.map($(".row"), function(el, index) {
  // return a function to push to `"enlighten"` queue
  return function(next) {
    // set queue name for `.cell input` elements to `"cells"`
    // iterate `input` elements within `.cell` elements
    $({}).queue("cells", $.map($(".cell > input", el), function(cell) {
      // return a function to push to `"cells"` queue
      return function(_next) {
        // animate `.cell input` elements
        $(cell).delay(100).animate({
            "background-color" : "#ffa500"
        }, 500).delay(100) 
        .animate({
          "background-color" : "#ffffff"
        })
        // when current `.cell input` element animation completes
        // call `_next` function in `"cells"` queue
        .promise().then(_next)           
      }
    // call first function in `"cells"` queue
    // when all `"cells"` queue functions have been called
    // call `next` function in `"enlighten"` queue
    })).dequeue("cells").promise("cells").then(next)
  }
// call first function in `"enlighten"` queue
})).dequeue("enlighten").promise("enlighten")
// do stuff when all functions in `"enlighten"` and `"cells"`
// queues have been called
.then(function() {
  console.log("all animations complete")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script   src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js"   integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="   crossorigin="anonymous"></script>
first board
<div class="board">
  first row
  <div class="row">
    <div class="cell">
      <input type="number" data-column="0" data-line="0">
    </div>
    <div class="cell">
      <input type="number" data-column="1" data-line="0">
    </div>
    <div class="cell">
      <input type="number" data-column="2" data-line="0">
    </div>
    <div class="cell">
      <input type="number" data-column="3" data-line="0">
    </div>
  </div>
</div>
<br>
second board
<div class="board">
  second row
  <div class="row">
    <div class="cell">
      <input type="number" data-column="0" data-line="0">
    </div>
    <div class="cell">
      <input type="number" data-column="1" data-line="0">
    </div>
    <div class="cell">
      <input type="number" data-column="2" data-line="0">
    </div>
    <div class="cell">
      <input type="number" data-column="3" data-line="0">
    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

32 Comments

Can I use queue with the $each? like Martin posted?
@JoãoSilva Note, you can also wrap an array in jQuery() to iterate the array with .each(), e.g., $([10,20,30]).each(function(index, element) {console.log(element)})
You should be able to use $(arr).each(function(){ enlight($(this)); }); , possibly $(cells).each() with you existing javascript
This awnser is a litle bit overkill for this simple problem
|
1

The Array type in Javascript does not contain an each function but Jquery does so.

jQuery.each( array, callback )

With this in mind the solution is to use the Jquery each function:

if(arr.length !=0){
    $each(arr,function(){
        enlight($(this));
    });
}

Edit:

You can get more detailed information how to use the JQuery.each function at the officiel docs: http://api.jquery.com/jquery.each/

4 Comments

Can I use queue or another function to wait for enligh finish with the first element before jumping to the next one?
Yes you can. There is allready a nice source at stackoverflow: stackoverflow.com/questions/5202403/…
But with setTimeout I need to have a timer, that is what im trying to run out... there isnt possible to do a done in each interactionor something?
You're missing a period

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.