首页 > 解决方案 > 如何将变量提取到 jquery 函数中?

问题描述

我使用文本文件的内容创建表格。现在,我正在尝试使用 jquery 动态表,我需要逐行添加最新数据。因此,我将使用“setInterval”,然后检查 data.length 并将旧大小与当前大小进行比较,然后尝试添加新行。

虽然我已经在 jQuery(全局)中定义了变量,但在使用它时它看起来像一个新变量。如何从函数中排除变量?

var data = [];

window.addEventListener("load", function() {
  function editData() {
    $.get("collection.txt", function(txt) {
      $("#output").text(txt);
      console.log(typeof txt);
      textLen = txt.length;
      text = txt.replaceAll('{', '[');
      textReverse = text.replaceAll('}', ']');
      removeHexa = textReverse.replace(/\d+:/gi, '');
      removelast = removeHexa.replace(/\u0000/gi, '');
      cleanSpace = removelast.replace(/\s/gi, '');

      cleancommas = cleanSpace.replace(/,,],|],/gi, ']\n');
      veri = cleancommas.replaceAll('\n[', '\n');
      veri = veri.replaceAll(']\n', '\n');
      veri = veri.replace('[', '');

      dataArray = veri.split('\n');

      dataArray.forEach(function(item) {
        item_new = item.split(',')
        data.push(item_new);
      });
    });
  }

  function anotherFunction() {
    data.unshift("etc"); // I want to use here that I get variable from Jquery
  }
  // or ı can use here
  data.unshift("  ..  .. ");
}

标签: javascriptjquerycallback

解决方案


推荐阅读