我有一个带有循环的函数,该函数调用(如果条件为真)另一个函数,该函数 .push() 它将数据作为对象返回到数组中。发生的事情是,在第一次调用第二个函数后,第一个函数的循环停止,代码继续执行下一部分代码(chrome 控制台中没有错误,我推送了正确的对象)。问题是什么?
第一个功能:
if (window.localStorage.length > 1) {
track_items_for_chart = [];
for (i = 0; i < window.localStorage.length; i++) {
console.log(i);
var key_name = (window.localStorage).key(i);
var record_time = (window.localStorage.getItem(key_name));
if (record_time !== '[]') {
console.log("record_time !== '[]'");
if (key_name !== 'exp') {
console.log("key_name !== 'exp'");
if (key_name !== 'ripple-last-load') {
console.log("key_name !== 'ripple-last-load'");
var computed_info = get_total_km(key_name);
}
}
}
}
第二个功能:
function get_total_km($object_key) {
// Get all the GPS data for the specific workout
var data = window.localStorage.getItem($object_key);
// Turn the stringified GPS data back into a JS object
data = jQuery.parseJSON(data);
if (data) {
// Calculate the total distance travelled
total_km = 0;
for (i = 0; i < data.length; i++) {
if (i === (data.length - 1)) {
break;
}
total_km += gps_distance(data[i].coords.latitude, data[i].coords.longitude, data[i + 1].coords.latitude, data[i + 1].coords.longitude);
}
total_km_rounded = parseFloat(total_km.toFixed(2));
// Calculate the total time taken for the track
start_time = new Date(data[0].timestamp).getTime();
end_time = new Date(data[data.length - 1].timestamp).getTime();
total_time_ms = end_time - start_time;
total_time_s = total_time_ms / 1000;
final_time_m = Math.floor(total_time_s / 60);
final_time_s = Math.floor(total_time_s - (final_time_m * 60));
// console.log({total_km_rounded: total_km_rounded, final_time_m: final_time_m, final_time_s: final_time_s});
var time_mas = parseFloat(final_time_m + "." + final_time_s);
track_items_for_chart.push(total_km_rounded, time_mas);
}
}