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 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败