如何显示视频当前和持续时间,如 00:25/00:30?

Red*_*Red 3 html jquery html5-video

我想以不同的格式显示currentTime视频。duration

现在的显示方式是:

0:9 / 0:33

我希望它像这样显示:

00:09 / 00:33

或者如果视频时长超过 1 分钟

03:10 / 12:27

我现在拥有的当前代码

    video.on('timeupdate', function() {

        // Set to minute and seconds
        var time = video[0].currentTime;
        var minutes = Math.floor(time / 60);   
        var seconds = Math.floor(time);

        // Set the current play value
        currentTimer.text(minutes + ':' + seconds);
    });

    video.on('loadedmetadata', function() {

        // Set to minute and seconds
        var time = video[0].duration;
        var minutes = Math.floor(time / 60);   
        var seconds = Math.floor(time);  

        // Set the video duration
        durationTimer.text(minutes + ':' + seconds);
    });
Run Code Online (Sandbox Code Playgroud)

Que*_*ger 7

这段代码应该做你想要的:

function format(s) {
    var m = Math.floor(s / 60);
    m = (m >= 10) ? m : "0" + m;
    s = Math.floor(s % 60);
    s = (s >= 10) ? s : "0" + s;
    return m + ":" + s;
}

alert(format(120));
alert(format(250));
alert(format(31));
Run Code Online (Sandbox Code Playgroud)