M
M
mopah2017-04-25 16:30:30
JavaScript
mopah, 2017-04-25 16:30:30

Why does the browser freeze when processing a file with a JS script?

Good afternoon everyone!
The task was to pull out a description of goods from a third-party supplier's website. There were a lot of goods (more than 1000), I wrote a parser to collect descriptions of positions. The JS script collected information, when it collected it, it converted the data to JSON (used JSON.Stringify), sent the script to PHP and wrote it to a file, because JS cannot write to a file. I am now working with this file to parse descriptions, process them, etc. The file has the following structure:

[
["\n                                                    \n    <div><p><br></p><div>Всякое описание текста.</div><div><br>Производитель: Такой-то</div><div>           <br>Доп параметры.</div><div><br>Дата производства: 2012г.          <br><br>Еще что-то .</div><div> </div></div>\n\n                            \n            ","\n                                                    \n            <div class=\"ty-product-feature\">\n        <span class=\"ty-product-feature__label\">Страна:</span>\n\n                            \n        <div class=\"ty-product-feature__value\">Китай</div>\n        </div>\n    \n    \n                            \n            "],
["\n                                                    \n    <div><p><br></p><p>О товаре<br></p><div>Описание</div><div>Еще всякое.</div><div>Еще чуть-чуть текста            <br>Еще</div><div>Описание вновь              <br>Еще параметр</div></div>\n\n                            \n            ","\n                                                    \n            <div class=\"ty-product-feature\">\n        <span class=\"ty-product-feature__label\">Страна:</span>\n\n                            \n        <div class=\"ty-product-feature__value\">Китай</div>\n        </div>\n    \n    \n                            \n            "]
]

Here is a description of the goods pulled from the supplier's website (example for 2 products, there are more than 1000 of them). Initially, there was a two-dimensional array, for each product there were 2 descriptions. And it is in the file.
I start to disassemble, I load the file through Ajax, I want to pull out the bare text from the tags using jQuery, in theory, this is easy to do, but here comes the snag. Script:
jQuery.ajax({
      type: "GET",
      url: "price-list-parsed-BT_21-Apr-17--17-41-36.txt",
      dataType: "json",
      error: function() {
        console.log("Не вышло");
      },
      success: function(temp) {
        newArr = temp;
        for (var i=0; i<newArr.length; i++) {
          workArr[i] = [];
          for (var j=0; j<newArr[i].length; j++) {
            workArr[i][j] = jQuery(newArr[i][j]); //***
          }
        }
      }
    });

Because If I am getting data from a JSON file, then I need to convert it to an array, making all the elements jQuery elements, in order to use, for example, the html() method to extract text from tags. The Parse method is not possible here, because there are a lot of extra characters here, which the parser will stumble over. I loop through the file, make each element a jQuery object, write it to an array (the line marked with ***), and at this stage the browser hangs. Don't want json elements to become jQuery elements. I can't figure out why. Without conversion, the find, html, text, etc. methods are not available. What am I missing, in which direction to dig I can’t understand, please help, who can.
Thank you all in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2017-04-25
@sergiks

  • JS can write to a file - use node.js instead of a browser - see fs.writeFile() and others.
  • If you really want to in the browser, you can use the web worker - it's like a parallel process that does not slow down the page / interface.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question