Javascript 错误:未捕获的 ReferenceError:[function] 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10973706/
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-08-24 03:51:09  来源:igfitidea点击:

Javascript Error: Uncaught ReferenceError: [function] is not defined

javascript

提问by kukac67

I have two js files, both called by one HTML file.

我有两个 js 文件,都由一个 HTML 文件调用。

<script type="text/javascript" src="scripts/levelmovement.js"></script>
<script type="text/javascript" src="scripts/generation.js"></script>

Part of levelmovement.js :

levelmovement.js 的一部分:

function moveLevel(){
    if(firstreset == true){
        resetTime();
    }
    var time2 = new Date();
    var millis2 = time2.getTime();
    var millis3 = millis2 - millis1;
    poschange = Math.floor(millis3 / 5);
    for(i = 0; i < chunkpos.length; i++){
        temppos[i] = chunkpos[i] - poschange;
        if(temppos[i] <= -150){
            temppos[i] += 1200;
            generate(i);
        }
        pos = temppos[i];
        document.getElementById('chunk' + i).setAttribute('style','left: ' + pos + 'px;');
    }
}

The function "moveLevel()" is called like this:

函数“moveLevel()”是这样调用的:

window.onload = function(){
    gameLoop();
}

function gameLoop(){
    if(currentscreen == 'playing'){
        moveLevel();
    }
    setTimeout('gameLoop()',1);
}

The entire generation.js :

整个 generation.js :

var generatedtop;
var howtogentop = 'gen';
var howtogenbottom = 'gen';
var chunktogenerate = 0;

function topGen(g){
    document.getElementById('t' + chunktogenerate).setAttribute('src','images/terrain/t' + g + '.png');
    if(g == 'gap0'){
        howtogentop = 'gap';
    }
    else{
        howtogentop = 'gen';
    }

    if(g == 'gap0' || g == 'gap2'){
        generatedtop = 'gap';
    }
    else{
        generatedtop = 'default';
    }
}

function bottomGen(g){
    document.getElementById('b' + chunktogenerate).setAttribute('src','images/terrain/b' + g + '.png');
    if(g == 'gap0'){
        howtogenbottom = 'gap';
    }
    else{
        howtogenbottom = 'gen';
    }
}

function generate(chunknum){
    chunktogenerate = chunknum;
    var rand1 = Math.floor(Math.random()*100)+1;
    var rand2 = Math.floor(Math.random()*100)+1;
    if(howtogentop == 'gen'){
        if(rand1 <= 25){
            topGen('space');
        }
        if(rand1 <= 50 && rand1 > 25){
            topGen('jump');
        }
        if(rand1 <= 75 && rand1 > 50){
            topGen('slide');
        }
        if(rand1 > 75){
            topGen('gap0');
        }
    }
    if(howtogentop == 'gap'){
        topGen('gap2');
    }

    if(howtogenbottom == 'gen'){
        if(generatedtop == 'gap'){
            if(rand2 <= 33){
                bottomGen('space');
            }
            if(rand2 <= 66 && rand2 > 66){
                bottomGen('jump');
            }
            if(rand2 > 66){
                bottomGen('gap0');
            }
        }
        if generatedtop != 'gap'){
            if(rand2 <= 25){
                bottomGen('space');
            }
            if(rand2 <= 50 && rand2 > 25){
                bottomGen('jump');
            }
            if(rand2 <= 75 && rand2 > 50){
                bottomGen('jump');
            }
            if(rand2 > 75){
                bottomGen('gap0');
            }
        }
    }
    if(howtogenbottom == 'gap'){
        bottomGen('gap2');
    }
}

I have checked over everything and "moveLevel()" only works if i remove this line of code:

我已经检查了所有内容,并且“moveLevel()”只有在我删除这行代码时才有效:

generate(i);

It appears as if the browser cannot see the "generate()" function and I don't know why...

似乎浏览器看不到“generate()”函数,我不知道为什么......

回答by Danilo Valente

This line:

这一行:

if generatedtop != 'gap'){

is missing a bracket. The correct is:

缺少一个括号。正确的是:

if(generatedtop != 'gap'){

回答by mim

I came to this page searching for a solution to a similar issue I had. Although this page did not help directly but gave a direction to fix the problem.

我来到这个页面寻找解决我遇到的类似问题的方法。虽然这个页面没有直接帮助,但给出了解决问题的方向。

Here is the actual reason why this happens:

这是发生这种情况的实际原因:

When you have a function in a js file say file1.js, that you are calling in another js file say file2.js, even though they are called on the same html page, the function will not work if there is any js errorin the whole file1.js, its like the whole js file is not included at all if it has an error.

当你在一个js文件说file1.js一个功能,您呼叫的另一个js文件说file2.js,即使他们被称为相同的HTML页面,该功能将无法正常工作,如果有任何JS错误的整个file1.js,就像整个js文件如果有错误根本不包含在内

So the solution is to clean up all the js errors that you are getting in all the included js files.

因此,解决方案是清除您在所有包含的 js 文件中遇到的所有 js 错误。

Hope this helps someone.

希望这可以帮助某人。

回答by user1423172

That's because your calling the function before it is defined. Changing the order of loading your js files should fix the issue.

那是因为您在定义函数之前调用了该函数。更改加载 js 文件的顺序应该可以解决问题。

<script type="text/javascript" src="scripts/generation.js"></script>
<script type="text/javascript" src="scripts/levelmovement.js"></script>