weixin_33691700 2014-07-09 15:30 采纳率: 0%
浏览 14

$ .ajax中的可变可移植性

I get that A in ajax is asynchronous. But I can't find an alternatives to what i'm trying to do. I have a graph that I need to feed with a javascript array. This array is made from a csv import. So I'm trying to make a function that return an array with the function recupCSV():

data = [];
objet function (data, objet) {
  var jour = data[3].split("/");
  var start = new Date(jour[2], jour[1], jour[0]);
  var dtS = new Date(start.getFullYear(), start.getMonth(), start.getDate());
  jour = data[4].split("/");
  var end = new Date(jour[2], jour[1], jour[0]);
  var dt = new Date(end.getFullYear(), end.getMonth(), end.getDate());
  var workItem = {id: data[0],name: data[1],lane: data[2],start: dtS,end: dt ,desc: data[5]};
  objet.push(workItem);
}

function recupCSV(data) {
  donnee = [];
  $.ajax({
    url: "http://www.example.com/csvfile.csv",
    aync: false,
    success: function (csvd) {
      csv_as_array = $.csv2Array(csvd);
      for(var i in csv_as_array )
      objet(csv_as_array[i],data);
    },
    dataType: "text",
    complete: function () {
    }
  });
  return data;
}

I can see many reasons why it doesn't work, but I can't see any that would.. ;)

  • 写回答

1条回答 默认 最新

  • weixin_33711647 2014-07-09 19:53
    关注

    Ok sorry for all this mess, here's the code that actualy works :

    d3.csv("file.csv", function (error, data) { 
    csvAsArray = data;
    localStorage.setItem('anobjectwithnoname', JSON.stringify(csvAsArray));
    });
    

    then in some function :

    var csv = localStorage.getItem('anobjectwithnoname');
    var csvAsArray = JSON.parse(csv);
    

    this way i got the object out of the asynchronous "trap" ;) sorry again for the messy code. please don't be to harsh with try and fail type of learning

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵