Javascript:抓取一个数组并总结所有值

时间:2014-04-03 19:08:20

标签: javascript jquery

在我的项目中,用户可以为项目添加时间码入点和出点,项目会自动计算出时间码的总持续时间。但我想添加一个函数,它将占用所有可用的时间码持续时间,将它们转换为秒,将它们加在一起,然后将最终的数字转换回时间码并将其放入文本输入中。

这是我的代码,但我一直收到语法错误:

     function timeToSeconds(t) {
        var tc = t.split(':');
        return parseInt(tc[0])*3600 + parseInt(tc[1])*60 + parseInt(tc[2]);
    }

    function tcDuration(tcin, tcout) {
        function z(n){return (n<10?'0':'') + n;}
        var duration = timeToSeconds(tcout) - timeToSeconds(tcin);
        var hoursmins = Math.floor(duration / 60);
        return z(Math.floor(hoursmins/60)) + ':' + z(hoursmins % 60) + ':' + z(duration % 60);
    }

    // Run this function every time a film_tc_out cell is changed
    function film_tc_Duration() {
        if (document.getElementById("film_tc_in").value == '') {var film_tc_in = '00:00:00';} else { var film_tc_in = document.getElementById("film_tc_in").value;}
        if (document.getElementById("film_tc_out").value == '') {var film_tc_out = '00:00:00';} else { var film_tc_out = document.getElementById("film_tc_out").value;}
        document.getElementById("film_tc_duration").value = tcDuration(film_tc_in, film_tc_out);
    }

    // Run this function every time a src_tc_out cell is changed
    function src_tc_Duration() {
        if (document.getElementById("src_tc_in").value == '') {var src_tc_in = '00:00:00';} else { var src_tc_in = document.getElementById("src_tc_in").value;}
        if (document.getElementById("src_tc_out").value == '') {var src_tc_out = '00:00:00';} else { var src_tc_out = document.getElementById("src_tc_out").value;}
        document.getElementById("src_tc_duration").value = tcDuration(src_tc_in, src_tc_out);
    }

    // Run this function every time a src_wd_out cell is changed
    function src_wd_Duration() {
        if (document.getElementById("src_wd_in").value == '') {var src_wd_in = '00:00:00';} else { var src_wd_in = document.getElementById("src_wd_in").value;}
        if (document.getElementById("src_wd_out").value == '') {var src_wd_out = '00:00:00';} else { var src_wd_out = document.getElementById("src_wd_out").value;}
        document.getElementById("src_wd_duration").value = tcDuration(src_wd_in, src_wd_out);
    }

    function total_tc_Duration() {
    var val = document.getElementsByClassName('.asset_src_tc_duration');
    var total_tc = 0;
    var v;
    for (var i = 0; i < val.length; i++) {
        v = timeToSeconds(val[i]); 
        if (!isNaN(v)) total_tc += v;
    }
    return (total_tc);
    }

function updateAssetTimecode() {
    document.getElementById("timecode_total").value = total_tc_Duration();
}

更新:我已经重写For循环,看看是否有帮助 - 它现在给了我一个答案,虽然答案总是&#34; 0&#34;。它没有吐出任何错误,但它似乎认为变量val不是一个数字?

1 个答案:

答案 0 :(得分:0)

您的tcDuration功能无法正常工作。在进行分钟计算之前,您不会减去已计算的小时数,也不会减去秒数。

function tcDuration(tcin, tcout) {
    function z(n){return (n<10?'0':'') + n;}
    var duration = timeToSeconds(tcout) - timeToSeconds(tcin);
    var hoursmins = Math.floor(duration / 60);
    return z(Math.floor(hoursmins / 60)) + ":" + z(hoursmins % 60) + ":" + z(duration % 60);
}