2015-08-31 79 views
1

代碼與價值創造裏面CSV文件:如何延遲功能,持續10秒

function exportTableToCSV($table, filename) { 
alert(filename); 
var $rows = $table.find('tr:has(td)'), 

// Temporary delimiter characters unlikely to be typed by keyboard 
     // This is to avoid accidentally splitting the actual contents 
     tmpColDelim = String.fromCharCode(11), // vertical tab character 
     tmpRowDelim = String.fromCharCode(0), // null character 

     // actual delimiter characters for CSV format 
     colDelim = '","', 
     rowDelim = '"\r\n"', 

     // Grab text from table into CSV formatted string 
     csv = '"' + $rows.map(function (i, row) { 
      var $row = $(row), 
       $cols = $row.find('td'); 

      return $cols.map(function (j, col) { 
       var $col = $(col), 
        text = $col.text(); 

       return text.replace(/"/g, '""'); // escape double quotes 

      }).get().join(tmpColDelim); 

     }).get().join(tmpRowDelim) 
      .split(tmpRowDelim).join(rowDelim) 
      .split(tmpColDelim).join(colDelim) + '"', 

     // Data URI 
     csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv); 
     alert(csvData); 
    $(this) 
     .attr({ 
     'download': filename, 
      'href': csvData, 
      'target': '_blank' 
    }); 
} 

下面的代碼是點擊按鈕,將數據導出到CSV文件後使用。

$(".Approve").on('click', function (event) {  
    var that = this; // need to capture `this` context, it doesn't carry into the timer context 
setTimeout(function() { 
exportTableToCSV.apply(that, [ $('#myDiv>table'), 'XYZ.csv' ]); 
}, 10 * 100);  
    }); 

}); 

我申請的變化,現在超時工作,但XYZ.csv不能與數據下載。

回答

1

exportTableToCSV.apply已經執行相應的功能。你將這個函數的輸出放入一個定時器中。

而是執行此操作:

var that = this; // need to capture `this` context, it doesn't carry into the timer context 
setTimeout(function() { 
    var result = exportTableToCSV.apply(that, [ $('#myDiv>table'), 'ExportedBoxResult.csv' ]); 
    console.log(result); 
}, 10 * 1000); 

exportTableToCSV.apply(this[$('#myDiv>table'),'ExportedBoxResult.csv']); 

有一個語法錯誤,括號不正確匹配。你打算:?

exportTableToCSV.apply(this, [ $('#myDiv>table'), 'ExportedBoxResult.csv' ]); 
+0

他'this' – MinusFour

+0

它不工作 –

+0

@MinusFour這將使感後失蹤一個逗號。 Moubani可以更具描述性嗎?我無法讀懂頭腦。 – Halcyon