无法在while循环中激活功能

时间:2016-08-02 11:03:35

标签: javascript loops while-loop

我似乎有一个很简单的错误,我似乎无法弄明白。我有两个功能。一个允许通过数组进行定时迭代(慢速迭代),另一个只是将数组中的项组合成一个句子(testfunction)。我希望在while循环中调用这两个函数,以便while循环将在一天中的某些时间之间连续运行(这是Now_time变量)。

如果我在没有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);
}

1 个答案:

答案 0 :(得分:1)

&#34;完成&#34;您正在传递的函数 - 将其包含在函数调用中: -

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