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);
}