javascript setTimeout 不是函数错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17808054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:42:34  来源:igfitidea点击:

setTimeout is not a function Error

javascriptsettimeout

提问by

Hey guys I'm making a simple memory game. I used some setTimeout. Until a point, It was going good. After that I wanted to add a stopwatch. And bom. I got this error.

嘿伙计们,我正在制作一个简单的记忆游戏。我使用了一些 setTimeout。直到某一点,它进展顺利。之后我想添加一个秒表。和邦。我收到了这个错误。

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

未捕获的类型错误:对象 [object Object] 的属性“setTimeout”不是函数

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.

我已经在这里卡了两天了。请掌舵我

回答by Arun P Johny

You have a bug at the incrementfunction, 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);
    }

}

回答by giorgio

The problem is with this line of code:

问题在于这行代码:

window.setTimeout=( ... );

回答by Joachim Isaksson

setTimeoutis normally a function, not a variable. You're reassigning it to the value 100 in your statement;

setTimeout通常是一个函数,而不是一个变量。您在语句中将其重新分配给值 100;

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

Next time you try to call it, it's not a function anymore.

下次尝试调用它时,它不再是一个函数。