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 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64