You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决JavaScript中去除视频时长毫秒时的‘is not a function’错误

解决去除视频时长毫秒数时的"is not a function"错误

你遇到的问题根源很明确:video.currentTime返回的是数字类型的数值(单位是秒,小数部分代表毫秒),而setMilliseconds()是Date对象专属的方法,普通数字根本没法调用这个方法,所以才会报"is not a function"错误。

给你两种修正方案,按需选择:

方案一:直接取整去掉毫秒(最简单)

既然只是要去掉毫秒,直接对秒数取整就行,用Math.floor()或者parseInt()都可以:

var test = document.getElementById('test');
setInterval(function asas() {
    if(video.readyState > 0) {
        var minutes = parseInt(video.currentTime / 60, 10);
        var seconds = Math.floor(video.currentTime % 60); // 取整后自动去掉毫秒
        test.innerHTML = minutes + ":" + seconds;
    }
}, 100); // 注意:原代码的0100是八进制数(等价于十进制64),直接写100才是100毫秒间隔

方案二:用Date对象处理(适合后续有更多时间操作的场景)

如果之后需要对时间做更多格式化或操作,可以先把秒数转成Date对象再处理:

var test = document.getElementById('test');
setInterval(function asas() {
    if(video.readyState > 0) {
        var minutes = parseInt(video.currentTime / 60, 10);
        var seconds = video.currentTime % 60;
        // 创建一个初始时间为0的Date对象
        var timeObj = new Date(0);
        timeObj.setSeconds(seconds);
        timeObj.setMilliseconds(0); // 现在可以调用这个方法了
        var c1seconds = timeObj.getSeconds();
        test.innerHTML = minutes + ":" + c1seconds;
    }
}, 100);

内容的提问来源于stack exchange,提问作者Eliseo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:16:06