2016-08-02 18 views
0

Ich habe anscheinend einen ziemlich einfachen Fehler, den ich anscheinend nicht herausfinden kann. Ich habe zwei Funktionen. Einer, der eine zeitgesteuerte Iteration durch ein Array ermöglicht (langsame Iteration), und ein anderer, der die Elemente im Array nur zu einem Satz kombiniert (Testfunktion). Ich möchte, dass diese beiden Funktionen in einer while-Schleife aufgerufen werden, so dass die while-Schleife kontinuierlich zwischen den Zeiten am Tag ausgeführt wird (dies ist die Variable Now_time).Problem beim Aktivieren der Funktion in der While-Schleife

Wenn ich den Code ohne die While-Schleife ausführen, wird es ordnungsgemäß ausgeführt. Sobald ich die while-Schleife einführe, durchläuft sie nur das erste Element und nicht kontinuierlich das Array mit den Funktionen.

Irgendwelche Vorschläge?

//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); 
} 
+0

einen Verschluss über alle Variablen erstellen .... Folgen Sie dem Link, um Ihre Problem wird gelöst http://stackoverflow.com/questions/17491718/funktions-call-inside-loop-taking-only-last-iteration –

+0

Vielen Dank für das Feedback Suraj. Ich habe versucht, den vorgeschlagenen Link zu betrachten und die Lösung anzuwenden, aber es sind nur Fehler aufgetreten. –

Antwort

1

Es gibt Fehler in der „done“ Funktion, die Sie vorbei - es innerhalb der Funktionsaufruf enthalten, wie: -

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

Danke! Ich habe die Korrektur für den Code vorgenommen. Sollte jetzt zum Testen laufen. –