实作 thread,大概有这三种方法︰
1. 分段做
function createBatchProcess() {
var running, keys;
function start() {
if (running) {
return;
}
running = true;
...
...
keys = Object.keys(books);
setTimeout(next);
}
function next() {
if (!keys.length) {
return stop();
}
var key = keys.shift();
print(books[key]);
BatchTextImport(books[key], Word_Application);
setTimeout(next);
}
function stop() {
Word_Application.Quit();
Word_Application = null;
running = false;
}
return {
start: start
};
}
$("ProcessGo").click(createBatchProcess().start);
2. 用 generator
function* genProcess() {
...
...
for (var key in books) {
yield print(books[key]);
BatchTextImport(books[key], Word_Application);
}
Word_Application.Quit();
Word_Application = null;
}
var process = genProcess();
$("ProcessGo").click(function do(){
if (!process.next().done) {
setTimeout(do);
}
});
3. 用 Web Worker: http://is.gd/b0mcrA