在while循环中激活功能时出现故障
Trouble activating function in 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);
}
您正在传递的"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);
}
相关文章:
- 添加文字和评论功能更新Div
- JavaScript打印功能使日历停止工作
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 如何防止网页加载后自动启动功能
- 除修剪外的其他功能
- 悬停功能触发器
- 使用angularjs向浏览器发送servlet响应(下载功能)
- javascript.点击隐藏事件故障
- 删除CKEditor工具栏按钮,但不删除功能
- 异步facebook功能
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- jQuery滚动功能只工作一次
- Jquery&有故障的功能
- 如何在使用$.post发送&将数据与done&故障功能
- OL3-检测点击功能时出现故障
- 超时功能和jquery出现故障
- 网络风暴:'在浏览器中打开'功能故障
- 退色功能故障
- 在while循环中激活功能时出现故障
- 使用.show()和.hide()功能的JavaScript/JQuery代码的故障排除