很简单,不做过多解释了,直接上代码
完整代码
<script>
data() {
return {
startTime: 0,
intervalId: null,
}
}
methods: {
startCall() {
this.startTime = Date.now();
this.intervalId = setInterval(() => {
this.updateDuration();
}, 1000);
},
updateDuration() {
let elapsed = Date.now() - this.startTime; // 已经过的毫秒数
let hours = Math.floor(elapsed / 3600000);
let minutes = Math.floor((elapsed % 3600000) / 60000);
let seconds = Math.floor((elapsed % 60000) / 1000);
// 补零操作
hours = hours < 10 ? '0' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
this.dialTimeText = hours + ':' + minutes + ':' + seconds;
},
endCall() {
clearInterval(this.intervalId);
}
}
</script>