2

I have this : https://jsfiddle.net/ahhc9tjn/

The problem is about this two functions :

this.addShipTest1 = function() {
    this.revolutionTime += 0.5;
    this.stopAllShipCSS();
    this.addShip()
}

this.addShipTest2 = function() {
    this.revolutionTime += 0.5;
    this.stopAllShipCSS();

    var save = this;
    setTimeout(function() {
        save.addShip();
    }, 100);
}

Why do I have to wait to make this work ? I mean the functions are not asynchronous, so should

addShip()

Be call only after

stopAllShipCSS()

The problem is that, in addShipTest1, the stop is "call after" or "ignore"... (The JSFiddle show the problem).

Edit :

Solutions I tried, but that doesn't succeed :

(1)
$.when(this.stopAllShipCSS()).then(this.addShip());

(2)
var it = 0;
var save = this;
this.ships.forEach(function(ship) {
  ship.css({
    'animation':''
  });
  it++;
  if (it == save.ships.length) {
    save.addShip();
   }
});
Neok
  • 221
  • 2
  • 17
  • 3
    it's because they *are* asynchronous – Daniel Lizik Mar 03 '17 at 17:31
  • Can you show us what stopAllShipCSS() does? I get this all the time when I use Kendo, I think some of Kendo's calls are async. – Jackstine Mar 03 '17 at 17:32
  • which one ? and I tried to use this property $.when(this.stopAllShipCSS()).then(this.addShip()) but it doesn't work either – Neok Mar 03 '17 at 17:32
  • 1
    What exactly is the problem here? Your explanation is not really clear. What happens, and what is it that you expect to happen? – Pointy Mar 03 '17 at 17:34
  • The JSFiddle show the problem (click mutiple time on 'add ship' button), the first that is not using 'setTimeout' doesn't work... but i wan't him to work because there's not evident reason (for me) to wait 100 ms... – Neok Mar 03 '17 at 17:36
  • Take a look here. It will help you http://stackoverflow.com/questions/18983138/callback-after-all-asynchronous-foreach-callbacks-are-completed – Steeve Pitis Mar 03 '17 at 17:38
  • 1
    No, the fiddle does not show the problem. If I add a `console.log()` in `stopAllShipCSS()`, it always happens *before* `addShip()` is called. – Pointy Mar 03 '17 at 17:38
  • [Here is an update to the fiddle with added `console.log()` calls.](https://jsfiddle.net/ahhc9tjn/1/) – Pointy Mar 03 '17 at 17:41
  • (1) I tried this Steeve, but doesn't work... (2) This is why i'm confuse – Neok Mar 03 '17 at 17:45
  • @ThomasPetillot have you looked at the updated jsfiddle that I mentioned? I don't see any problem with the code. The `stopAllShipCSS()` function **is** called before `addShip()` and it finishes before `addShip()`. – Pointy Mar 03 '17 at 18:08
  • Yes, thats what I said i was confuse... Because, if this order of call is good, the two function should give the same result, but they didn't ... – Neok Mar 03 '17 at 18:10
  • I really can't found why i'm not doing right... – Neok Mar 03 '17 at 18:29

0 Answers0