繁体   English   中英

Javascript函数从数组返回固定值

[英]Javascript function returning fixed value from array

我是JavaScript的新手,由于缺乏官方网页上的信息,因此我决定开发一个小应用程序,该应用程序应显示我所住地方的有轨电车的时间表。

该行的开始时间有几个数组,到达每个站点的时间相同,因此我只需要将总分钟数加到第一个小时。

用户可以通过表格来设置小时数范围。 因此,我的主要问题是“ adder();” 函数应该迭代并打印数组中的所有值。 取而代之的是,它始终使用相同的索引24,因此,如果返回的数组的索引少于24,则它将不起作用。

这是HTML:

<input type =“ button” class =“ submit” value =“ Enviar” onclick =“ caller()” />

JavaScript:

function cropHours(i){

        if (i.substr(0,2) >= hora1user_recortada && i.substr(0,2) <= hora2user_recortada) {
            horas.push(i);
        }
        return horas;
 }

function adder() {
    minInicio1 = horas[i].substr(0,2);
    minInicio2 = horas[i].substr(3,2);
    document.getElementById("test4").innerHTML = "---" + minInicio1+"_"+minInicio2;
    y = parseInt(total) + parseInt(minInicio2);
   document.getElementById("test5").innerHTML = "total vale "+total+"minInicio1 vale "+minInicio1+"... minInicio2 vale "+minInicio2+"...Y vale "+y;
    html += "<td>"+y+"</td>";
    document.getElementById("horario").innerHTML = html;
}

这是另一个功能的一部分:

if (platform == 1) {
    for (var j = 0; j <= indexorigen; j++) {
        total += mins1[j];
    }
    for (var j = 0; j <= indexdestino; j++) {
        total2 += mins1[j];
    }
    if (today !== "Sábado" || today !== "Domingo") {
        for each (var i in horainiciolaboral1) {
            cropHours(i);
            //adder(horainiciolaboral1);
        }
    } else {

        for each (var i in horainiciofinde1) {
            cropHours(i);
        }
    }
} else {
    for (var x = 0; x <= indexorigen; x++) {
            total += mins2[x];
        }
        for (var x = 0; x <= indexdestino; x++) {
            total2 += mins2[x];
        }
    if (today !== "Sábado" || today !== "Domingo") {
        for each (var i in horainiciolaboral2) {
            cropHours(i);
        }
    } else {
        for each (var i in horainiciofinde2) {
            cropHours(i);
        }
    }
}

/*for (var i = 0; i <= horainiciolaboral1.length; i++) {
    adder(horainiciolaboral1);
}*/

//horario = horas.slice(11);
for each (var i in horas) {
    adder();
}
document.getElementById("test6").innerHTML = horas;
document.getElementById("test3").innerHTML = total + "----" + total2;

// ******************************************
// ** FUNCTION WHICH CALLS EVERY FUNCTION  **
// ******************************************
// STARTS
function caller() {
cleaner();
retrieve_origen(); 
retrieve_destino(); 
getIndex(); 
sumMinutes(); 
getHours();
}

这就是问题所在:对于每个(hori中的var i){adder(); }

先感谢您。

i作为参数传递给adder()

adder(i);

...并将其定义为函数中的参数:

function adder( i ) {
   //...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM