2016-08-02 5 views
0

私は理解できないようなかなり単純なエラーがあるようです。私には2つの機能があります。配列(遅い反復)を介して時限反復を可能にするものと、配列内の項目を文(test関数)に結合するもの。これらの2つの関数をwhileループで呼び出すことで、whileループが1日に何回か連続して実行されるようにします(これはNow_time変数です)。whileループで問題を起動する機能

whileループなしでコードを実行すると、正しく実行されます。 whileループを導入するとすぐに、最初の要素を反復し、関数を使用して配列を連続的に反復するのではありません。

提案がありますか?関数呼び出しの内部でそれを含める: - -

//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

は....すべての変数の上にクロージャを作成するリンクをたどって、あなたの問題は解決される http://stackoverflow.com/questions/17491718/function-call-inside-loop-taking-only-last-iteration –

+0

フィードバックSurajをありがとう。私は、提案されたリンクを見て、ソリューションを適用しようとしましたが、ただエラーに遭遇しました。 –

答えて

1

あなたが渡している「完了」機能にエラーがあり

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

ありがとう!私はコードを訂正しました。テストのために今すぐ実行する必要があります。 –

関連する問題