简体   繁体   English

javascript函数移至webworker

[英]javascript function move to webworker

I have function: 我有功能:

function addModel() {

    var values = new Array();
    var $input = $('input[type=\'text\']');
    var error = 0;
    $input.each(function() {
        $(this).removeClass('double-error');
        var that = this;
        if (that.value!='') {
            values[that.value] = 0;
            $('input[type=\'text\']').each(function() {
                if (this.value == that.value) {
                    values[that.value]++;
                }
            });
        }
    });

    $input.each(function(key) {
        if (values[this.value]>1) {
            //error++;
        var name = this.value;
        var product_model = $(this).parent().parent().find('.product-model').text();
        var m = product_model.toLowerCase().areplace(search,replace);
     $(this).parent().find('input[type=\'text\']').val(name + '-' + m);

        }

    }); 


    return error <= 0; //return error > 0 ? false : true;
}

where are a lot of inputs to recheck... up to 50000. Usually are about 5000 to 20000 inputs. 哪里有很多要重新检查的输入...多达50000。通常大约是5000到20000输入。 Of course browsers are freezing... How to move this function to web-worker and call it to get data back and fill form type="text" 当然,浏览器会冻结...如何将此功能移至网络工作者并调用它以取回数据并填写表格type =“ text”

thank you in advance. 先感谢您。

Web workers don't have direct access to the DOM. Web工作者没有直接访问DOM的权限。 So to do this, you'd need to gather the values of all 5-50k input s into an array or similar, and then send that to the web worker (via postMessage ) for processing, and have the web worker do the relevant processing and post the result back, and then use that result to update the input s. 因此,要做到这一点,您需要将所有5-50k input的值收集到一个数组或类似的数组中,然后将其发送给Web Worker(通过postMessage )进行处理,然后让Web Worker进行相关处理。并将结果发回,然后使用该结果更新input s。 See any web worker tutorial for the details of launching the worker and passing messages back and forth (and/or see my answer here ). 有关启动工作程序以及来回传递消息的详细信息,请参阅任何Web工作程序教程(和/或在此处查看我的答案 )。

Even just gathering up the values of the inputs and posting them to the web worker is going to take significant time on the main UI thread, though, as is accepting the result from the worker and updating the input s; 但是,即使只是收集输入值并将其发布到Web Worker上,也要在主UI线程上花费大量时间,就像接受来自worker的结果并更新input s一样。 even 5k input s is just far, far too many for a web page. 甚至5k input s对于网页来说也远远不够。

If maintaining browser responsiveness is the issue, then releasing the main thread periodically will allow the browser to process user input and DOM events. 如果问题是保持浏览器的响应能力,则定期释放主线程将使浏览器可以处理用户输入和DOM事件。 The key to this approach is to find ways to process the inputs in smaller batches. 这种方法的关键是找到小批量处理输入的方法。 For example: 例如:

var INTERVAL = 10; // ms
var intervalId = setInterval((function () {
  var values = [];
  var $input = $('input[type=\'text\']');
  var index;
  return function () {
    var $i = $input[index];
    var el = $i.get();
    $i.removeClass('double-error');
    if (el.value != '') {
      values[el.value] = 0;
      $input.each(function() {
        if (this.value == el.value) {
          values[el.value]++;
        }
      });
    }
    if (index++ > $input.length) {
      clearInterval(intervalId);
      index = 0;

      // Now set elements
      intervalId = setInterval(function () {
        var $i = $input[index];
        var el = $i.get();
        if (values[el.value] > 1) {
          var name = el.value;
          var product_model = $i.parent().parent().find('.product-model').text();
          var m = product_model.toLowerCase().areplace(search,replace);
          $i.parent().find('input[type=\'text\']').val(name + '-' + m);
        }
        if (index++ > $input.length) {
          clearInterval(intervalId);
        }
      }, INTERVAL);
    }
  };
}()), INTERVAL);

Here, we do just a little bit of work, then use setInterval to release the main thread so that other work can be performed. 在这里,我们只做一点工作,然后使用setInterval释放主线程,以便可以执行其他工作。 After INTERVAL we will do some more work, until we finish and call clearInterval INTERVAL之后,我们将做更多的工作,直到完成并调用clearInterval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM