1

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

Anh Tuan Nguyen
  • 429
  • 1
  • 6
  • 18

1 Answers1

1

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);
}
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
  • It only works, when I use an anonymous function setTimeout(function(){getDistance(kundenList[i],helferList[j])},(500 * k++)); But then I have the problem, that the parameters are unknown or null when passed to getDistance. – Anh Tuan Nguyen Jul 13 '16 at 08:15
  • I don't have enough information to help you with that error. The problem of delay is solved with my answer. To solve the other problem, please, create a new question with that. – Marcos Pérez Gude Jul 13 '16 at 09:38
  • 1
    I figured out the problem! I have to pass the getDistance() Function as a string. setTimeout('getDistance("'+kundenAddress+'","'+helferAddress+'")',(2000 * k++)); – Anh Tuan Nguyen Jul 18 '16 at 12:55
  • That's a scope problem, glad to hear it's solved. :) – Marcos Pérez Gude Jul 18 '16 at 12:56