JS时分秒计算,每秒自动执行,支持开始及清除

HTML代码:

<span>通话时间: <span id="callTime">00:00:00</span></span>

JS代码:
var secondIndex = 0; // 初始值
var callTimesInter; // 定时器

times = {

	/**
	 * 时分秒换算函数
	 * @param {Object} value 秒
	 */
 	formatSeconds: function(value){
        var secondTime = parseInt(value); // 秒
        var minuteTime = 0; // 分
        var hourTime = 0; // 小时
        if(secondTime > 60) {
            minuteTime = parseInt(secondTime / 60);
            secondTime = parseInt(secondTime % 60);
            if(minuteTime > 60) {
                hourTime = parseInt(minuteTime / 60);
                minuteTime = parseInt(minuteTime % 60);
            }
        }
        var secondTimes = (parseInt(secondTime) < 10 ? "0" + parseInt(secondTime) : parseInt(secondTime));
        var result = "00:00:" + secondTimes;
        if(minuteTime > 0){
        	var minuteTimes = (parseInt(minuteTime) < 10 ? "0" + parseInt(minuteTime) : parseInt(minuteTime));
            result = "00:" + minuteTimes + ":" + secondTimes;
        }
        if(hourTime > 0){
        	var hourTimes = (parseInt(hourTime) < 10 ? "0" + parseInt(hourTime) : parseInt(hourTime));
            result = hourTimes + ":" + minuteTimes + ":" + secondTimes;
        }
        return result;
    },
	
	// 通话时间初始化
	callTimeLoad: function(flag){
		if(flag){
			callTimesInter = setInterval(function(){
				secondIndex++;
				let time = times.formatSeconds(secondIndex);
				$("#callTime").text(time);
			}, 1000);
		}else{
			clearInterval(callTimesInter);
			secondIndex = 1;
		}
	},
	
}
 

函数调用:
times.callTimeLoad(true); // 开始时间计算
times.callTimeLoad(false); // 结束时间计算

效果截图: