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 三极管电路求解,已知电阻电压和三级关放大倍数
  • ¥15 ADS时域 连续相位观察方法
  • ¥15 Opencv配置出错
  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused