在while循环中激活功能时出现故障

Trouble activating function in while loop

本文关键字:故障 功能 激活 while 循环      更新时间:2023-09-26

我似乎有一个很简单的错误,我似乎无法理解。我有两个功能。一个允许通过数组进行定时迭代(慢速迭代),另一个只将数组中的项组合成一个句子(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);
}

您正在传递的"done"函数中存在错误-将其包含在函数调用中,如下所示:-

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