简体   繁体   English

为什么我在这里得到“setTimeout 不是函数”?

[英]Why do I get “setTimeout is not a function” here?

I am making a simple memory game.我正在制作一个简单的记忆游戏。 I used some setTimeout.我使用了一些 setTimeout。 Until a point, It was going good.直到某一点,它进展顺利。 After that I wanted to add a stopwatch.之后我想添加一个秒表。 And bom.和邦。 I got this error.我收到了这个错误。

Uncaught TypeError: Property 'setTimeout' of object [object Object] is not a function

At this line:在这一行:

function counter(){

ctxCounter.font="100pt Arial";
var fillT= setTimeout( function(){ctxCounter.fillText("3",270,340);} , 100);
var clearT= setTimeout( function(){counterClear();} , 1000);  
}

and my stopWatch part's code.和我的秒表部分的代码。 After adding this line I get the error written above.添加此行后,我收到上面写的错误。 But without stopwatch everything works perfectly.但是没有秒表,一切都完美无缺。

var timer=0;
var running=false;
    

    function startPause(){
        if(running==false){
            running=true;
            increment();
        }
        else running=false;             
    }

    function reset(){
        timer=0;
        running=false;
    }


    function increment(){
        if(running==true){
            
            window.setTimeout=(

                function(){
                var mins= Math.floor(timer/600);
                var secs= Math.floor(timer/10);
                var tenths= timer%1000;
                var all= mins+":"+secs+":"+tenths;
                console.log(all);
                increment();
                },100);
        }

    }

I've stuck here for two days.我在这里卡了两天。 Please, helm me.请掌舵我

问题在于这行代码:

window.setTimeout=( ... );

You have a bug at the increment function, there was a = after window.setTimeout你有一个increment函数的错误,在window.setTimeout之后有一个=

function increment(){
    if(running==true){

        window.setTimeout(

            function(){
            var mins= Math.floor(timer/600);
            var secs= Math.floor(timer/10);
            var tenths= timer%1000;
            var all= mins+":"+secs+":"+tenths;
            console.log(all);
            increment();
            },100);
    }

}

setTimeout is normally a function, not a variable. setTimeout通常是一个函数,而不是一个变量。 You're reassigning it to the value 100 in your statement;您在语句中将其重新分配给值 100;

window.setTimeout=(function(){...}, 100);

Next time you try to call it, it's not a function anymore.下次尝试调用它时,它不再是一个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM