Notify After Async Task Is Done
Solution 1:
Because animations in javascript are asynchronous (they run via timers), the only way to know when they are done is to hook into some sort of completion callback or completion notification.
I don't know move.js myself, but it looks like you can get such a callback by supplying a callback to the .end(fn)
method. But, you will also have to fix your code because this
in this.checkIfWin()
will not be the right value in either of these code blocks. I don't know what you want it to be (since you don't show that part of your code), but you will have to refer to some other object besides this
. In any case, here's the general structure of the code both with and without the use of a deferred.
var deferred = new $.Deferred();
// your code was using this here // so I will save a reference to it that can be used later, but there is// probably a better way to do this if I could see the rest of your codevar obj = this;
click(e);
//show message when game is over
deferred.promise().then(function(){
if (obj.checkIfWin()){
alert('you won !');
obj.nextLevel();
}
});
functionclick(e){
move(e)
.set('background-color','black')
.delay('0.1s')
.end(function() {
deferred.resolve();
});
}
In this case, it doesn't look like you really need to use the deferred as you could just put the completion code right in the callback:
// your code was using this here // so I will save a reference to it that can be used later, but there is// probably a better way to do this if I could see the rest of your codevar obj = this;
click(e);
functionclick(e){
move(e)
.set('background-color','black')
.delay('0.1s')
.end(function() {
if (obj.checkIfWin()){
alert('you won !');
obj.nextLevel();
}
});
}
Solution 2:
Edit, Added callback function to .end()
to include deferred.notify
. See comments , Move#end([fn])
Try
var deferred = new $.Deferred();
click(e); // `e` undefined ?
deferred.progress(function(msg) {
// `this` within `deferred`// references `deferred` object ,// try `obj.checkWin()`, `obj.nextLevel()`if (obj.checkWin()) {
alert(msg);
obj.nextLevel();
}
});
functionclick(e) {
// `e` undefined ?move(e)
.set('background-color','black')
// `.delay()` accepts `number` as argument ?
.delay('0.1s')
// see comments , // http://visionmedia.github.io/move.js/#example-11
.end(function() {
deferred.notify("you won !");
});
};
Post a Comment for "Notify After Async Task Is Done"