旋转轮-动画功能即使旋转停止也能继续进行

时间:2018-10-05 19:21:07

标签: javascript

我正在使用javascript函数和画布以及动画函数来旋转轮子(轮盘式的旋转轮)。

停止旋转时,我正在尝试查看获得的奖金。那部分工作正常。

现在,我要将奖品传递给另一个功能,从那里我要将奖品名称发送到后端。但是,当我尝试console.log奖品时,我得到了正确的详细信息,但它确实通过了无限循环,因为我的功能还在继续。

这是功能:

(function anim() {
    deg += speed;
    deg %= 360;

    // Increment speed
    if(!isStopped && speed<3){
        speed = speed+1 * 0.1;
    }
    // Decrement Speed
    if(isStopped){
        if(!lock){
            lock = true;
            slowDownRand = rand(0.994, 0.998);
        } 
        speed = speed>0.2 ? speed*=slowDownRand : 0;
    }
    // Stopped!
    if(lock && !speed){
        var ai = Math.floor(((360 - deg - 90) % 360) / sliceDeg); // deg 2 Array Index
        ai = (slices+ai)%slices; // Fix negative index
        // return alert("You got:\n"+ label[ai] ); // Get Array Item from end Degree
        submitEntry(label[ai]);    
    }

    drawImg();
    window.requestAnimationFrame( anim );
}());

我想我知道window.requestAnimationFrame(anim)不断重复调用该函数的原因,并且只有lock and !speed变为true的条件,但是函数一直持续不断。

一旦停止旋转,如何停止功能或中断功能或退出功能?

0 个答案:

没有答案