Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击按钮时暂停了requestAnimationFrame_Javascript_Animation_Requestanimationframe - Fatal编程技术网

Javascript 单击按钮时暂停了requestAnimationFrame

Javascript 单击按钮时暂停了requestAnimationFrame,javascript,animation,requestanimationframe,Javascript,Animation,Requestanimationframe,是否可以暂停请求动画帧? 当我单击特定按钮时,动画必须暂停。 在需要继续播放动画时,再次单击该按钮 public addProgressbar() { const progressbar = $('.progressbar__value'); const max = this.$progressMax; const time = (1000 / max) * 5; let start = null; this.progress = 0; cons

是否可以暂停
请求动画帧
? 当我单击特定按钮时,动画必须暂停。 在需要继续播放动画时,再次单击该按钮

public addProgressbar() {
    const progressbar = $('.progressbar__value');
    const max = this.$progressMax;
    const time = (1000 / max) * 5;
    let start = null;
    this.progress = 0;

    const step = timestamp => {
        if (start === null) {
            start = timestamp;
        }
        this.progress = timestamp - start;
        progressbar[0].style.width = Math.min(this.progress / 50, 2000).toFixed() + '%';
        if (this.progress < 5000) {
            requestAnimationFrame(step);
        }
    };
    setTimeout(() => {
        window.requestAnimationFrame(step);
    }, 1000);
}
public addProgressbar(){
常量progressbar=$('.progressbar__值');
const max=此值。$progressMax;
常数时间=(1000/最大值)*5;
让start=null;
这个进度=0;
常量步骤=时间戳=>{
如果(开始===null){
开始=时间戳;
}
this.progress=时间戳-开始;
progressbar[0].style.width=Math.min(this.progress/502000).toFixed();
如果(该进度小于5000){
请求动画帧(步骤);
}
};
设置超时(()=>{
window.requestAnimationFrame(步骤);
}, 1000);
}

如果问题中的代码有效,我不明白你为什么要问这个问题。代码很有效,不是吗?是的,我编辑了文本。代码运行得很好,但我需要暂停并继续动画的功能。
requestAnimationFrame
不像
setInterval
,它不会连续运行。换句话说:你不能“暂停”一个动画帧的请求,你只能取消它。可以避免请求附加动画帧。这就是你的意思吗?当我取消动画帧时,例如在50%时,我可以在50%时再次开始吗?如果是的话,那对我来说是个好办法