热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

等待webworker完成吗?-Javascript-Waitforwebworkertofinish?

IamimplementingtheLDAalgorithmofTopicModellinginjavascriptasapartofaproject.Uponth

I am implementing the LDA algorithm of Topic Modelling in Javascript as a part of a project. Upon the click of a button, the function to perform LDA is called. However since this is a very heavy task, the browser used to hang for around 15 seconds when the function was called (even the loading animated gif wasn't working while processing was taking place).

作为项目的一部分,我正在用Javascript实现主题建模的LDA算法。单击按钮时,将调用执行LDA的函数。然而,由于这是一项非常繁重的任务,所以当调用该函数时,浏览器通常会挂起大约15秒(即使是正在处理的加载动画gif也无法工作)。

So I implemented the function as a web worker. This solves the hanging problem but now the loading animation disappears within a couple of seconds while the results are shown some 12-14 seconds after that.

所以我以web worker的形式实现了这个功能。这解决了挂起问题,但现在加载动画在几秒内消失,而结果显示在12-14秒之后。

Is there any way I can make the loader animate while the processing is going on ? I mean wait for the web worker to finish computing before the loading animation disappears.

在处理过程中,有没有什么方法可以让加载器动画?我的意思是在加载动画消失之前等待web worker完成计算。

The script is called asynchronously and the loader is attached to ajax events of jQuery as

脚本被异步调用,加载程序被附加到jQuery as的ajax事件上

$('#spinner').bind("ajaxSend", function() {
   $(this).show();
}).bind("ajaxComplete", function() {
   $(this).hide();
});

1 个解决方案

#1


3  

Activate the spinner before inititating the (heavy) worker logic, and disable the spinner once the worker is finished:

在初始化(重)工作程序逻辑之前,激活微调控制程序,并在完成工作后禁用微调程序:

var worker = new Worker('workerscript.js');
worker.Onmessage= function(event) {
    // Do something with event.data, then hide the spinner.
    $('#spinner').hide();
};
$('#spinner').show();
worker.postMessage({args: ' foo bar '});

workerscript.js would look like this:

workerscript。js是这样的:

self.Onmessage= function(event) {
    var results;
    // do something
    ...
    // Done:
    postMessage(results);
};

推荐阅读
author-avatar
董可芳妍_731
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有