简体   繁体   English

Javascript错误:未捕获ReferenceError:未定义[功能]

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

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()”函数,我也不知道为什么...

This line: 这行:

if generatedtop != 'gap'){

is missing a bracket. 缺少括号。 The correct is: 正确的是:

if(generatedtop != 'gap'){

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 error in 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. 希望这对某人有帮助。

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>

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

相关问题 ANGULARJS / JAVASCRIPT:错误-未捕获的ReferenceError:函数未定义 - ANGULARJS/JAVASCRIPT : error - Uncaught ReferenceError: function is not defined Javascript-未捕获的ReferenceError:函数未定义 - Javascript - Uncaught ReferenceError: function is not defined Javascript Uncaught ReferenceError:函数未定义 - Javascript Uncaught ReferenceError: Function is not defined Javascript:未捕获的ReferenceError:函数未定义 - Javascript : Uncaught ReferenceError: function is not defined JavaScript-未捕获的ReferenceError:我的函数未定义 - JavaScript - Uncaught ReferenceError: my function is not defined 未捕获的ReferenceError:onclick函数Javascript上未定义变量 - Uncaught ReferenceError: variable is not defined on onclick function Javascript Javascript函数:未捕获ReferenceError:未定义potDeposit - Javascript function: Uncaught ReferenceError: potDeposit is not defined JavaScript - 未捕获的ReferenceError:checkAnswer未定义,函数不在范围内? - JavaScript - Uncaught ReferenceError: checkAnswer is not defined, function not in the scope? 未定义的未捕获referenceerror函数 - uncaught referenceerror function is not defined javascript android Javascript 错误 Ubuntu Uncaught ReferenceError: $ is not defined - Javascript Error in Ubuntu Uncaught ReferenceError: $ is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM