异步任务完成后通知

我正在使用move.js构建一个用于动画的游戏,但是它花费的时间太长,所以当玩家点击正确的解决方案时,它会在正确的解决方案更改它的颜色之前显示获胜的消息,所以我使用延迟对象来触发事件和动画结束时抓住它。

这是我的代码:

 var deferred = new $.Deferred(); click(e); //show message when game is over $.when(deferred).then(function(){ if (this.checkIfWin()){ alert('you won !'); this.nextLevel(); } }); function click(e){ move(e) .set('background-color','black') .delay('0.1s') .end(); deferred.notify(); } 

但它没有得到通知,消息没有显示出来。我在这里错过了什么?

因为javascript中的动画是异步的(它们通过计时器运行),所以知道它们何时完成的唯一方法是挂钩某种完成回调或完成通知。

我自己不知道move.js,但看起来你可以通过向.end(fn)方法提供回调来获得这样的回调。 但是,您还必须修复代码,因为this.checkIfWin()中的代码在这些代码块中都不是正确的值。 我不知道你想要它是什么(因为你没有显示你的部分代码),但你必须引用除此之外的其他一些对象。 在任何情况下,这里是使用和不使用延迟的代码的一般结构。

 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 code var obj = this; click(e); //show message when game is over deferred.promise().then(function(){ if (obj.checkIfWin()){ alert('you won !'); obj.nextLevel(); } }); function click(e){ move(e) .set('background-color','black') .delay('0.1s') .end(function() { deferred.resolve(); }); } 

在这种情况下,看起来你真的不需要使用延迟,因为你可以将完成代码放在回调中:

 // 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 code var obj = this; click(e); function click(e){ move(e) .set('background-color','black') .delay('0.1s') .end(function() { if (obj.checkIfWin()){ alert('you won !'); obj.nextLevel(); } }); } 

编辑,添加回调函数到.end()以包含deferred.notify 。 看评论, 移动#结束([fn])

尝试

 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(); } }); function click(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 !"); }); }; 

jsfiddle http://jsfiddle.net/guest271314/4Av4Z/