在函数开头调用时,ClearTimeout不起作用

Cak*_*ake 0 javascript settimeout cleartimeout

我有一个带有setTimeout的函数.该函数的第一部分将超时设置为清除,以防它在setTimeout触发之前再次调用它.

clearTimeout不会清除计时器.我已经在调用了setTimeout的函数中的部分之后测试了clearTimeout(spinTimer)并且它可以工作.为什么一开始不起作用?

我已经全局声明了变量:

var spinTimer;

function cakeCubeSpin(whereTo){

    clearTimeout(spinTimer);

    ... do some stuff

    switch(whereTo){
    case 1:
        var spinTimer = setTimeout( function(){
            $('#ccPanel4').css(leftReset);
            $('#ccPanel2').css(rightReset);
            $('#ccPanel3').css(backReset);
            $('#ccPanel1').css(frontReset);

            $('#cakeCubeWrapper').css(wrapReset);

            $('#ccPanel1').addClass('cc-current');
        }, ccSpeed);

    break;

    ... more stuff

}
Run Code Online (Sandbox Code Playgroud)

xan*_*ded 5

您将使用以下行重新确定范围 spinTimer:

var spinTimer = setTimeout( function(){
Run Code Online (Sandbox Code Playgroud)

我想你的意思是:

spinTimer = setTimeout( function(){
Run Code Online (Sandbox Code Playgroud)