使用链式 setTimeout() 模拟播放器

在这里插入图片描述

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BIM施工模拟</title>
    <style>
        .progress-bar {
            width: 0;
            height: 20px;
            background-color: springgreen;
        }
        .btn-play {
            display: inline-block;
            width: 30px;
            height: 30px;
            color: tomato;
            background: transparent url('./icon-play.svg') no-repeat center center;

        }
        .btn-play.active {
            background-image: url('./icon-pause.svg');
        }
        .btn-stop {
            display: inline-block;
            width: 30px;
            height: 30px;
            background: transparent url('./icon-stop.svg') no-repeat center center;
        }
    </style>
</head>
<body>
    <div style="background-color:teal">
        <div class="progress-bar" id="progress-bar"></div>
        <div>
            <a class="btn-play" href="javascript:0;"></a>
            <a class="btn-stop" href="javascript:0;"></a>
        </div>
    </div>    
<script>

var iTotalTime = 60; // 总时间/秒
var iTotalFrames = 1200; // BIM部件总帧数
var iCurFrame = 0; // 已播放帧
var isPlay = ""; // 是否播放
var btnPlay = document.getElementsByClassName('btn-play')[0];
var btnStop = document.getElementsByClassName('btn-stop')[0];
var progressbar = document.getElementById('progress-bar');
btnPlay.onclick = function(){
    btnPlay.classList.toggle('active');
    if(this.className.indexOf('active') > -1){
        isPlay = true;
        setTimeout(function(){
            if(iCurFrame == iTotalFrames){
                btnPlay.classList.remove('active');
            }
            else{  
                if(isPlay){ 
                    iCurFrame++;
                    progressbar.style.width = iCurFrame / iTotalFrames * 100 + '%'; // 直接使用会导致闪烁                    
                    /** 其它处理事件
                    * 显示部分BIM部件...
                    */
                    setTimeout(arguments.callee, iTotalTime*1000/iTotalFrames);
                }
            }
        }, iTotalTime*1000/iTotalFrames)

    }else{
        isPlay = false;
    }
}
btnStop.onclick = function(){
    isPlay = false;
    iCurFrame = 0;
    progressbar.style.width = 0;
    btnPlay.classList.remove('active');
    /** 其它处理事件
    * 显示全部BIM部件...
    */
}

</script>
</body>
</html>

ES5严格模式下用下面方法:

var iTimer = null; // 定时器
var iTotalTime = 60; // 总时间/秒
var iTotalFrames = 1200; // BIM部件总帧数
var iCurFrame = 0; // 已播放帧
var isPlay = ""; // 是否播放
var btnPlay = document.getElementsByClassName('btn-play')[0];
var btnStop = document.getElementsByClassName('btn-stop')[0];
var progressbar = document.getElementById('progress-bar');
btnPlay.onclick = function () {
    btnPlay.classList.toggle('active');
    if (this.className.indexOf('active') > -1) {
        isPlay = true;
        fnProcessingProgress();
    } else {
        isPlay = false;
    }
}
btnStop.onclick = function () {
    iTimer = null;
    isPlay = false;
    iCurFrame = 0;
    progressbar.style.width = 0;
    btnPlay.classList.remove('active');
    /** 其它处理事件
    * 显示全部BIM部件...
    */
}
function fnProcessingProgress() {
    iTimer = window.setTimeout(function () {
        if (iCurFrame == iTotalFrames) {
            btnPlay.classList.remove('active');
        }
        else {
            if (isPlay) {
                iCurFrame++;
                progressbar.style.width = iCurFrame / iTotalFrames * 100 + '%'; // 直接使用会导致闪烁                    
                /** 其它处理事件
                * 显示部分BIM部件...
                */
                setTimeout('fnProcessingProgress()', iTotalTime * 1000 / iTotalFrames); // OR setTimeout(arguments.callee, iTotalTime*1000/iTotalFrames);
                
            }
        }
    }, iTotalTime * 1000 / iTotalFrames)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值