Reputation: 439
I have function that reads a csv file and calculates the distance between two types of values "Client" and "Helper"
function readCSV(e){
var file = e.target.files[0];
var helferList =[];
var kundenList =[];
if (!file) {
console.log('file could not be read');
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
var result = $.csv.toArrays(contents);
$('.output').append(",");
for(i = 0; i< result.length; i++){
if(result[i][0] =="Kunde"){
kundenList.push(result[i]);
}
else if(result[i][0] =="Helfer"){
helferList.push(result[i]);
$('.output').append(result[i][1] + " "+ result[i][2] + ", ");
}
}
$('.output').append("\n");
console.log(kundenList.length);
for(i = 0; i< kundenList.length; i++){
$('.output').append(kundenList[i][1] + " "+ kundenList[i][2] + ", ");
for(j=0; j <helferList.length;j++){
setTimeout(getDistance(kundenList[i],helferList[j]),500);
}
$('.output').append("\n");
}
};
reader.readAsText(file);
}
The problem I have is that I must have a delay between each distance calculation. For this reason I use:
setTimeout(getDistance(kundenList[i],helferList[j]),500);
But it does not seem to work as there is no delay between the calculations
Upvotes: 1
Views: 62
Reputation: 22158
You must to use a multiplicator because in your current code all functions will be fired passed 500 miliseconds, but not incremental. After 500 miliseconds of the for()
execution all functions will be fired instantly.
To change this, make a multiplicator with a flag like this:
function readCSV(e){
var file = e.target.files[0];
var helferList =[];
var kundenList =[];
if (!file) {
console.log('file could not be read');
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
var result = $.csv.toArrays(contents);
$('.output').append(",");
for(i = 0; i< result.length; i++){
if(result[i][0] =="Kunde"){
kundenList.push(result[i]);
}
else if(result[i][0] =="Helfer"){
helferList.push(result[i]);
$('.output').append(result[i][1] + " "+ result[i][2] + ", ");
}
}
$('.output').append("\n");
console.log(kundenList.length);
var k = 0;
for(i = 0; i< kundenList.length; i++){
$('.output').append(kundenList[i][1] + " "+ kundenList[i][2] + ", ");
for(j=0; j <helferList.length;j++){
// 500 * k = 500 * 1 | 500 * 2 | etc
setTimeout(getDistance(kundenList[i],helferList[j]),(500 * k++));
}
$('.output').append("\n");
}
};
reader.readAsText(file);
}
Upvotes: 1