0

I seem to be having quite a simple error that I cannot seem to figure out. I have two functions. One that allows for a timed iteration through an array (slow iteration) and another that just combines the items in the array into a sentence (testfunction). I want these two functions to be called in a while loop so that the while loop will continually run between times in the day (this is the Now_time variable).

If i run the code without the while loop it runs correctly. As soon as I introduce the while loop it only iterates the first element and not continuously through the array using the functions.

Any suggestions?

    //Sample array
var data = [
{Name:"Cape Town",City_Type:"Seaside"}
,
{Name:"Johannesburg",City_Type:"Inland"}
,
{Name:"Durban",City_Type:"Seaside"}
,
{Name:"Bloemfontein",City_Type:"Inland"}
];

// Slowly iterates through a given array
function slowArrayIterate(array, iterateFunc, start, speed) {
    iterateFunc(array[start], start, array);
    if (typeof array[start + 1] !== 'undefined') {
        setTimeout(function() {
            slowArrayIterate(array, iterateFunc, start + 1, speed, done);
        }, speed);
    } else {
        done();
    }
}
// Forms the sentence from the elements in the array
function testfunction(a,b){
  var complete = a +" is a " + b +" city.";
  console.log(complete);
}

// Gets the time of the day
var myDate = new Date(Date.now());
var time_now = myDate.toString().substring(16,24);
var here = time_now;
var there = time_now;
var everywhere = time_now;
var now = here.substring(0,2)+ there.substring(3,5) + here.substring(6,8);
var Now_time = parseFloat(now);

while (Now_time >= 73000 || Now_time <=170000) {
  console.log("working");
  // Calls the fucntion to slowly iterate
  slowArrayIterate(data, function(arrayItem) {

      console.log(arrayItem.Name);
      console.log(arrayItem.City_Type);
      // Calls the fucntion to form a sentence on the console log
      testfunction(arrayItem.Name , arrayItem.City_Type);
  }, 0, 1000);
// refreshes the time to see if it should still be running
  myDate = new Date(Date.now());
  var time_now = myDate.toString().substring(16,24);
  var here = time_now;
  var there = time_now;
  var everywhere = time_now;
  var now = here.substring(0,2)+ there.substring(3,5) + here.substring(6,8);
  var Now_time = parseFloat(now);
  console.log(Now_time);
}
  • create a closure over all the variables ....Follow the link your problem will be solved http://stackoverflow.com/questions/17491718/function-call-inside-loop-taking-only-last-iteration – Suraj Rawat Aug 02 '16 at 11:05
  • Thanks for the feedback Suraj. i tried looking at the suggested link and applying the solution but just ran into errors. – Claudio Clouds Wyss Aug 02 '16 at 12:58

1 Answers1

1

There is error in the "done" function you are passing - include it inside the function call as :-

var data = [
{Name:"Cape Town",City_Type:"Seaside"}
,
{Name:"Johannesburg",City_Type:"Inland"}
,
{Name:"Durban",City_Type:"Seaside"}
,
{Name:"Bloemfontein",City_Type:"Inland"}
];

// Slowly iterates through a given array
function slowArrayIterate(array, iterateFunc, start, speed, done) {
    iterateFunc(array[start], start, array);
    if (typeof array[start + 1] !== 'undefined') {
        setTimeout(function() {
            slowArrayIterate(array, iterateFunc, start + 1, speed, done);
        }, speed);
    } else {
        done();
    }
}
// Forms the sentence from the elements in the array
function testfunction(a,b){
  var complete = a +" is a " + b +" city.";
  console.log(complete);
}

// Gets the time of the day
var myDate = new Date(Date.now());
var time_now = myDate.toString().substring(16,24);
var here = time_now;
var there = time_now;
var everywhere = time_now;
var now = here.substring(0,2)+ there.substring(3,5) + here.substring(6,8);
var Now_time = parseFloat(now);

while (Now_time >= 73000 || Now_time <=170000) {
  console.log("working");
  // Calls the fucntion to slowly iterate
  slowArrayIterate(data, function(arrayItem) {

      console.log(arrayItem.Name);
      console.log(arrayItem.City_Type);
      // Calls the fucntion to form a sentence on the console log
      testfunction(arrayItem.Name , arrayItem.City_Type);
  }, 0, 1000, function() { //problem was here
      // stuff to do when finished (not important for now)
      console.log("Done");
  });
// refreshes the time to see if it should still be running
  myDate = new Date(Date.now());
  var time_now = myDate.toString().substring(16,24);
  var here = time_now;
  var there = time_now;
  var everywhere = time_now;
  var now = here.substring(0,2)+ there.substring(3,5) + here.substring(6,8);
  var Now_time = parseFloat(now);
  console.log(Now_time);
}
Anmol Mittal
  • 843
  • 5
  • 12