【发布时间】:2014-06-09 10:24:11
【问题描述】:
我在 a 中使用 Q 对某些文本行 (demo) 进行一些 jQuery 顺序处理。现在我添加了一个暂停按钮来暂时停止forEach 循环内的处理,但是如果我在while-循环内添加一个promise.delay(300)(参见下面的部分代码),我的浏览器就会挂起。如何使用 Q 在 forEach 循环中对布尔值 isPaused 实现条件暂停?
var promise = new Q();
promise = promise.then(function () {
lines.forEach(function (item) {
// Browser starts to hang here if isPaused == true
promise = promise.then(function () {
while (isPaused) {
promise = promise.delay(300);
}
});
// The following does not work either
// while (isPaused) {
// promise = promise.delay(300);
//}
// The following does not work either
// while (isPaused) {
// Q.delay(300);
//}
if (item[0] == '%') {
promise = promise.then(function ()
{ return addPrompt(wnd); })
promise = promise.then(function ()
{ return addInput(item.substr(1)); })
}
else {
promise = promise.then(function ()
{ return addOutput(item, wnd); })
}
});
promise = promise.then(function ()
{ return addPrompt(wnd); })
});
promise.done();
【问题讨论】: