我正在尝试让我的库在jQuery 3.0中运行,但由于Deferred对象中的更改无法正常工作,我的代码如下:
function ready(defer) {
return function(fun) {
if (defer.state() !== 'resolved') {
defer.then(fun).fail(function(e) {
self.error(e);
});
} else {
fun.call(fun);
}
};
}
我有两种方法暂停和恢复
pause: function(visible) {
if (self.id() == 4) {
console.log('pause call');
}
cmd_ready(function ready() {
onPause();
paused = true;
if (self.id() == 4) {
console.log('pause', paused);
}
command_line.disable();
if (!visible) {
command_line.hidden();
}
if ($.isFunction(settings.onPause)) {
settings.onPause.call(self);
}
});
return self;
},
resume: function() {
if (self.id() == 4) {
console.log('resume call');
}
cmd_ready(function ready() {
paused = false;
if (self.id() == 4) {
console.log('resume', paused);
}
if (terminals.front() === self) {
command_line.enable();
} else {
if (self.id() == 4) {
console.log('not front');
}
}
command_line.visible();
var original = delayed_commands;
delayed_commands = [];
for (var i = 0; i < original.length; ++i) {
self.exec.apply(self, original[i]);
}
self.trigger('resume');
var fn = resume_callbacks.shift();
if (fn) {
fn();
}
scroll_to_bottom();
if ($.isFunction(settings.onResume)) {
settings.onResume.call(self);
}
});
return self;
},
需要延迟,因为我在创建command_line对象之前执行暂停。
我在第一次暂停后调用resolve:
if (self.id() == 4) {
console.log('resolve');
}
command_defer.resolve();
以及这样的日志顺序:
pause call
resolve
resume call
resume false
resume call
resume false
pause true
订单应如下所示:
pause call
resolve
pause true
resume call
resume false
resume call
resume false
为什么暂停然后在调用resolve后调用很长时间的回调?它在jQuery 2中工作。