复杂/耗时操作Javascript中的setTimeout

时间:2014-04-03 05:30:20

标签: javascript user-interface settimeout

我有一个名为BookMarkReplacementAndPrint()的复杂函数。此功能使用activex在客户端浏览器上从服务器打开Word文档。并取代 书标记,并将打印命令发送到打印机以打印文档。 有几个操作非常耗时,比如打开文档,替换书签。我已经标记了那段代码。

现在,在UI的同时,我需要通过更新textarea来显示活动的进度,并且有一个"关闭"按钮关闭操作。 问题是我的UI一旦启动就没有响应。

如何让我的UI响应?在每个TimeConsuming任务之后,我尝试使用setTimeout()。但仍然使用较少。有没有更好的方法呢?

先谢谢......

var bookMarks = new Array(); 
bookMarks[0]="txtCustAllTLName";       
bookMarks[1]="txtAccountType";
bookMarks[2]="txtAcntNbr";
bookMarks[3]="dteFulfilmentCreated";


var bookMarksValue = new Array(); 
bookMarksValue[0]="Sample Name";       
bookMarksValue[1]="Savings";
bookMarksValue[2]="XXXXX5804";
bookMarksValue[3]="22/01/2008";
function BookMarkReplacementAndPrint()
{
 for (i=0;i<30;i++)
 {
     try{

      var empty;
      var doc = new ActiveXObject("Word.Application"); // creates the word object
      var sDocPath = "http://localhost:8080/bm1.doc";
      doc.Visible=false; // doesn't display Word window

      LOG('Opening the document file');

      //............Time consuming........................................
      // Open the document
      var oDoc = doc.Documents.Open(sDocPath, false, true);
      // .................................................................

      doc.ActivePrinter = "Printer Name";
      doc.Options.PrintBackground = true;

      //............Time consuming.......................................
      // Replacing the Bookmarks.
     for (i=0;i<bookMarks.length;i++)
     {
          LOG("Book Mark Name: "+bookMarks[i] + " " + "Book Mark Value: "+ bookMarksValue[i]);
          DisplayTextInfo('Book Mark Name: "+bookMarks[i] + " " + "Book Mark Value: '+ bookMarksValue[i]);
          var selection = doc.Selection.GoTo(-1,empty,0,bookMarks[i]);
          doc.Selection.TypeText(bookMarksValue[i]);
     }
      // .................................................................

     // Send to Printer
       /* Start Printing */
      doc.ActiveDocument.PrintOut();
      var textArea =  document.getElementById('myarea');
          textArea.innerHTML += "Printed document  "+ i + "\n";
    }
    catch(e)
    {
        alert(e.message);
    }
    finally
    {  doc.quit(0); // quit word (very important or you'll quickly chew up memory!)
    }
  }
}

0 个答案:

没有答案