javascript - 将时间分成分钟和秒

标签 javascript

我有以下代码,它应该可以满足我的需要:

function fromSeconds(seconds, showHours = false) {
    if(showHours) {
        var hours = Math.floor(seconds / 3600),
            seconds = seconds - hours * 3600;
    }
    var minutes = (Math.floor(seconds/60) < 10) ? 
        "0" + Math.floor(seconds/60) : Math.floor(seconds/60);
    var seconds = (seconds % 60 > 9) ? seconds % 60 : "0" + seconds % 60;

    if(showHours) {
        var timestring = hours + ":" + minutes + ":" + seconds;
    } else {
        var timestring = minutes + ":" + seconds;
    }
    return timestring;
}

问题是我也有这个:

var video = $('#home_explainer_placeholder');
video.bind("timeupdate", function() {
    $('#currentTime').html(video[0].currentTime.toFixed(2));
    $('#remTime').html((video[0].duration - video[0].currentTime).toFixed(2));
    $('#totalTime').html(video[0].duration.toFixed(2));
});

而且我不知道如何应用第一个代码,例如 currentTime 显示如下:minutes:seconds

有什么帮助吗?

最佳答案

通过一个小的固定,你可以让它保持原样:

Demo

function fromSeconds(seconds, showHours) {
    if(showHours) {
        var hours = Math.floor(seconds / 3600),
            seconds = seconds - hours * 3600;
    }
    var minutes = ("0" + Math.floor(seconds/60)).slice(-2);
    var seconds = ("0" + parseInt(seconds%60,10)).slice(-2);

    if(showHours) {
        var timestring = hours + ":" + minutes + ":" + seconds;
    } else {
        var timestring = minutes + ":" + seconds;
    }
    return timestring;
}

var video = $('#home_explainer_placeholder');
video.bind("timeupdate", function () {
    $('#currentTime').html(fromSeconds(video[0].currentTime));
    $('#remTime').html(fromSeconds(video[0].duration - video[0].currentTime));
    $('#totalTime').html(fromSeconds(video[0].duration));
});

关于javascript - 将时间分成分钟和秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29966382/

相关文章:

javascript - 如何查找具有特定数据属性值的元素

javascript - 如何在 HighCharts 中为堆栈柱形图格式化我的 json 数据

javascript - 使用 ajax、php 加载更多帖子

javascript - 使用ajax和php将值多次插入数据库

javascript - 'returning a promise' 生成的 promise 中 'resolving with promise' 和 `new` 之间的不同行为

javascript - 我想让 v-card 透明,但它不能正常工作

javascript - jQuery .abort() on keyup 事件

javascript - 如何将 FileReader base64 分配给变量?

javascript - api 代理服务器不将数据解析回用户 CORS

javascript - 如何实现只选择一个 block ,而不是所有 block ?