0

I'm using Slickgrid to display data on a html site. The user can choose a table and the columns.

The code works well, but one table contains around 30 columns and around 500000 rows. Now the script takes too long and I get a firefox javascript timeout.

I know, that i can use setTimeout(), but i don't know how to use in this function. What can i do, to avoid the javascript timeout?

    function addRow(){
      for (var i=0;i<arrayRow.length;i++){
        var row ='{"id": "' + i + '", ';
        for (var j=0;j>arrayColumn.length;j++){
          row = row + '"' + arrayColumn[j] + '" : "' + array[j]+[i] + '",'
        }
        row = row.substr(0,row.length-1);
        row = row + '}';
        data[i]=JSON.parse(row);
      }
      if (i==arrayRow.length){
        dataView.setItems(data);
      }
    }

Edit1: I've updated my code, but now I get the error "too much recursion".

    i=0;
    function addRow(){
      if (i<arrayRow.length){
        var row ='{"id": "' + i + '", ';
        for (var j=0;j>arrayColumn.length;j++){
          row = row + '"' + arrayColumn[j] + '" : "' + array[j]+[i] + '",'
        }
        row = row.substr(0,row.length-1);
        row = row + '}';
        data[i]=JSON.parse(row);
        i++;
        if(i%100000==0){
          setTimeout(addRow,0);
        } else {
          addRow();
        }
      }
      if (i==arrayRow.length){
        dataView.setItems(data);
      }
    } 
Jakbac
  • 101
  • 7
  • possible duplicate of [Prevent long running javascript from locking up browser](http://stackoverflow.com/questions/672732/prevent-long-running-javascript-from-locking-up-browser) – Ole Haugset Oct 02 '14 at 11:10

1 Answers1

0

The timeout for script execution measure the time that took the execution of one method. You need to separete "for" loop into several loops. For example you can make a method that add 100000 rows and call it with settimeout 5 times

Maybe it should be something like that:

var iFirstRow = 0;  
funtion AddRows( _iFirstRow, _nRowsToAdd ) 
{
   ..implementation of adding nRows 
   iFirstRow = iFirstRow  + nRowsToAdd;

   if ( There is rows to add )
   {
      Call AddRows via timeout
   }
   else
   {
      ..its ready
   }
}
Azzy Elvul
  • 1,403
  • 1
  • 12
  • 22