簡體   English   中英

Javascript錯誤:未捕獲ReferenceError:未定義[功能]

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

我有兩個js文件,都由一個HTML文件調用。

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

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;');
    }
}

函數“ moveLevel()”的調用方式如下:

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

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

整個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');
    }
}

我已經檢查了所有內容,並且只有刪除以下代碼行才能使用“ moveLevel()”:

generate(i);

好像瀏覽器看不到“ generate()”函數,我也不知道為什么...

這行:

if generatedtop != 'gap'){

缺少括號。 正確的是:

if(generatedtop != 'gap'){

我來到此頁面,尋找與我類似的問題的解決方案。 盡管此頁面沒有直接幫助,但提供了解決問題的指導。

這是發生這種情況的實際原因:

當您在一個js文件中說一個函數file1.js時,您正在另一個js文件中說一個file2.js調用,即使它們是在同一個html頁面上調用的, 如果在其中出現任何js錯誤 ,該函數也將不起作用。 如果有錯誤,則完全不包括整個file1.js,就像整個js文件一樣。

因此,解決方案是清除所有包含的js文件中遇到的所有js錯誤。

希望這對某人有幫助。

那是因為您在定義函數之前就調用了它。 更改加載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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM