0

I'm looping through a .csv and it works.

The problem is that i need to slow down my Loop, because the server (later in my code) has a request limit. I tried delay()-functions like this, but they just slowed everything down. So I decided to us a localHost to slow the URL itself down - which works.

The problem: It is not one "data"going through the loop/ getting delayed/ goes on etc.

Much "data" goes through the loop at the same time - so I slow them all down at once and still reach the serverlimit.

Question 1: is there an easy way to delay(), or to say the loop that only one "data" can pass at once?

Question2: If not: I thought about limiting the loop to .csv-line 1 To put it in another loop that repeats line by line - I could not figure out how to code this - i'm new to P5 and new to Code

My Code

function gotData(data) {
var route = data.features; 
for (var j = 0; j <= 1; j++ ) {  
var citydata = Städte[j].split(/,/);   
var lon = citydata[3];
var lat = citydata[2];

loadJSON('http://localhost:4567/2000/https://api.openrouteservice.org/directions?api_key='+ Key +'&coordinates=' + 
          lon + ',' + lat + '|11.789879,50.1905748&profile=' + profile + '&preference=' + preference + '&format=geojson', getData);
beginShape(); 
     for (var i = 0; i < route[0].geometry.coordinates.length; i=i+500) {
     var x = route[0].geometry.coordinates[i][0];
     var y = route[0].geometry.coordinates[i][1];       
     noFill();
     vertex(x*100-200,-y*100+6000);            
      } 
  endShape();   
}         
}

So I want the for "j" to loop through the hole .csv j<Städte.length. But then i reach my request limit.

In fact I need to ask for one Line of the .csv / delay it / send it and so on

How can i create a 2sec break after every line in my .csv?

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
foliran
  • 75
  • 6

1 Answers1

0

Normally I would recommend using the millis() function or the frameCount variable to perform timing logic in P5.js. Searching the tag for those keywords should get you a bunch of results.

But in your case, you could use the setTimeout() function to trigger the next iteration of the "loop". Something like this:

var currentIndex = 0;

function setup() {
    nextIteration();
}

function nextIteration(){

    console.log('current index: ' + currentIndex);

    // Do something with the current index here

    currentIndex++;

    // Call the nextIteration() function in 1 second
    setTimeout(nextIteration, 1000);
}

But honestly, this is a hack to get around what seems like an overly restrictive quota. If I were you, I'd look into increasing your quota. Or maybe you could pre-process the data just one time instead of fetching it from scratch every time?

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Hacking it would be okay for now, I'm already trying to get a higher Quota. Untill then: I don't know where to use the function "nextInteraion" and what means "do someting with the current index"? thanks for all your help – foliran Oct 21 '18 at 18:55
  • @foliran It means, instead of doing this in a `for` loop, you'd use the `nextIteration()` function along with the `setTimeout()` function to "loop" over your data by increasing a variable each time it's called. – Kevin Workman Oct 21 '18 at 18:56
  • Okay i think i get the Idea. starting the "loop" und using setTimeout to let it begin after 1 sec. But i dont get it to work. I think its the console.log - what am i supposed to change here? – foliran Oct 21 '18 at 19:06
  • @foliran My code is just an example. You would do whatever you want with the index, in your case probably request or process the data at that index? – Kevin Workman Oct 21 '18 at 20:08
  • Okay, I got it. I guess i can't use this Hack. Here i would call the function nextIteration under/in the gotData function. The "loop" is called under gotData(data) – foliran Oct 21 '18 at 23:39